while-loop不使用两个if语句

时间:2015-11-09 18:52:40

标签: java loops if-statement while-loop statements

我试过在网上搜索,我不能。基本上我想做一个程序,你可以选择你想要的饮料,然后当你输入饮料时,它会告诉它多少。我使用了一个while循环,所以如果你输入错误的价格,它会一直要求合适的价格直到你选择它。它适用于我的第一杯饮料,即#pepsi"但是当我选择可乐时,代码根本就没有运行......它甚至没有要求价格。

import java.util.Scanner;
public class BendingMachineTestWhile{



 public static void main (String [] args)
  {
    String drink;
    int money;
    Scanner kbd = new Scanner (System.in);

System.out.println (" Pick from the following drinks: \n pepsi \n coke \n gatorade");
drink = kbd.nextLine();
while (true){     
  if (drink.equals ("pepsi"))
  {
    System.out.println ("That will be 4 dollars");
    System.out.println ("Enter 4 dollars");
    money = kbd.nextInt();
    if ( money == 4 )
    {
      System.out.println ("Thanks.");
      break;
    }
    else if ( money != 4)
    {
      System.out.println ("You idiot its four dollars");
    }

  }
}

if (drink.equals ("coke"))
{
  System.out.println ("That will be 6 dollars");
  System.out.println ("Enter 6 dollars");
  money = kbd.nextInt();
  while (true){
    if ( money == 6 )
    {
      System.out.println ("Thanks.");
      break;
    }
    else if ( money != 6)
    {
      System.out.println ("You idiot its four dollars");
    }
  }

}
}
}

第二个if语句是不起作用的语句。如果你们能帮助我,我们将非常感激。

3 个答案:

答案 0 :(得分:1)

问题是coke选择的代码超出了while循环。因此,它永远不会执行甚至检查循环执行中的焦炭。

我将您的代码格式化了一下以便更容易阅读。 :)

顺便说一句,你的代码是"焦炭"病情与你的百事可乐状况不同。我会将它们改为基本相同,因为它们应该执行相同的基本操作。

将你的循环改为:

while (true){     
    if (drink.equals ("pepsi"))
    {
        System.out.println ("That will be 4 dollars");
        System.out.println ("Enter 4 dollars");
        money = kbd.nextInt();
        if ( money == 4 )
        {
            System.out.println ("Thanks.");
            break;
        }
        else
        {
            System.out.println ("You idiot it's four dollars");
        }
    }
    else if (drink.equals ("coke"))
    {
        System.out.println ("That will be 6 dollars");
        System.out.println ("Enter 6 dollars");
        money = kbd.nextInt();
        if ( money == 6 )
        {
            System.out.println ("Thanks.");
            break;
        }
        else
        {
            System.out.println ("You idiot it's four dollars");
        }

    }
    else {
        System.out.println("You didn't make a valid choice, please choose coke or pepsi");
    }

    // Get another input to continue program, here.
}

您应该考虑将变量命名为描述其目的的内容。例如,您的Scanner应该命名为:input。这使得它非常清楚它的作用,然后暗示如何根据它所使用的对象来使用它。

答案 1 :(得分:0)

在(真实)陈述时尝试将它放在你的内心。

同样作为更现实的场景,您可以制作一个包含价格和名称的数组,但这完全是另一个问题

答案 2 :(得分:0)

我认为这对你有好处。

import java.util.Scanner;

public class BendingMachineTestWhile {

public static void main(String[] args) {
    String drink;
    int money;
    Scanner kbd = new Scanner(System.in);

    System.out.println(" Pick from the following drinks: \n pepsi \n coke \n gatorade");
    drink = kbd.nextLine();
    while (true) {
        if (drink.equals("pepsi")) {
            System.out.println("That will be 4 dollars");
            System.out.println("Enter 4 dollars");
            money = kbd.nextInt();
            if (money == 4) {
                System.out.println("Thanks.");
                break;
            } else if (money != 4) {
                System.out.println("You idiot its four dollars");
            }

        } else if (drink.equals("coke")) {
            System.out.println("That will be 6 dollars");
            System.out.println("Enter 6 dollars");
            money = kbd.nextInt();
            if (money == 6) {
                System.out.println("Thanks.");
                break;
            } else if (money != 6) {
                System.out.println("You idiot its four dollars");
            }
        } else {
            if (drink.equals("gatorade")) {
                System.out.println("That will be 8 dollars");
                System.out.println("Enter 8 dollars");
                money = kbd.nextInt();
                if (money == 8) {
                    System.out.println("Thanks.");
                    break;
                } else if (money != 8) {
                    System.out.println("You idiot its four dollars");
                }
            } else {
                System.out.println("Your entered drink is not available here so please enter from the list again");
                System.out.println(drink = kbd.nextLine());
            }
        }

    }
}
}