while 循环不适用于两个 if 语句
while-loop isn't working with two if-statements
我试过到处在线搜索,但我不能。基本上我想做一个程序,你可以选择你想要的饮料,然后当你输入饮料时,它会告诉你有多少。我使用了一个 while 循环,所以如果你输入错误的价格,它会一直要求正确的价格,直到你 select 它。它适用于我的第一杯饮料 "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 语句不起作用。如果你们能帮助我,我将不胜感激。
问题是您的 coke
选择代码在 while 循环之外。因此,它从不执行,甚至不检查循环执行中的可乐。
我对您的代码进行了一些格式化,以便于阅读。 :)
顺便说一句,您的 "coke" 条件代码与百事可乐条件不同。我会将它们更改为基本相同,因为它们应该执行相同的基本操作。
将循环更改为:
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
。这使得它的作用非常清楚,然后暗示如何根据它的对象使用它。
试着把它放在你的 while(true) 语句中。
此外,作为更现实的场景,您可以制作一个包含价格和名称的数组,但这完全是另一个问题
我认为这对你有好处。
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());
}
}
}
}
}
我试过到处在线搜索,但我不能。基本上我想做一个程序,你可以选择你想要的饮料,然后当你输入饮料时,它会告诉你有多少。我使用了一个 while 循环,所以如果你输入错误的价格,它会一直要求正确的价格,直到你 select 它。它适用于我的第一杯饮料 "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 语句不起作用。如果你们能帮助我,我将不胜感激。
问题是您的 coke
选择代码在 while 循环之外。因此,它从不执行,甚至不检查循环执行中的可乐。
我对您的代码进行了一些格式化,以便于阅读。 :)
顺便说一句,您的 "coke" 条件代码与百事可乐条件不同。我会将它们更改为基本相同,因为它们应该执行相同的基本操作。
将循环更改为:
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
。这使得它的作用非常清楚,然后暗示如何根据它的对象使用它。
试着把它放在你的 while(true) 语句中。
此外,作为更现实的场景,您可以制作一个包含价格和名称的数组,但这完全是另一个问题
我认为这对你有好处。
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());
}
}
}
}
}