我的 Do-While 循环不检查我的条件?
My Do-While loop doesn't check my condition?
我正在使用以下代码制作以下关于在菜单上下订单的方法:
public static double menu_received() {
Scanner input = new Scanner(System.in);
int userResponse, total = 0;
do {
System.out.println();
System.out.println("Here is our menu:");
System.out.println("1. Bulgogi -- .00");
System.out.println("2. Kalbi -- .00");
System.out.println("3. Kimchi Fried Rice -- .00");
System.out.println("Please choose one option at one time using the number (0 to end the order):");
userResponse = input.nextInt();
System.out.println("User Menu choice is " + userResponse);
if (userResponse == 1) {
System.out.println("Thank you for ordering Bulgogi");
total = total + 15;
System.out.println("Your total is $" + total);
}
if (userResponse == 2) {
System.out.println("Thank you for ordering Kalbi");
total = total + 18;
System.out.println("Your total is $" + total);
}
if (userResponse == 3) {
System.out.println("Thank you for ordering Kimichi Fried Rice");
total = total + 16;
System.out.println("Your total is $" + total);
}
} while (userResponse != 0);
double total2 = total + 2.55;
// total = (double) total + 2.55;
System.out.println("Your total after tax is $" + total2);
return total2;
}
但是我遇到的问题是,当我调用它时,它没有按我想要的方式响应。即使在我输入 0 后它也不会停止调用菜单。 while(userResponse != 0) 我的情况是否有问题有人可以向我解释为什么以及如何纠正它吗?谢谢
这是我的输出:
>
Here is our menu:
1. Bulgogi -- .00
2. Kalbi -- .00
3. Kimchi Fried Rice -- .00
Please choose one option at one time using the number (0 to end the order):
1
User Menu choice is 1
Thank you for ordering Bulgogi
Your total is
Here is our menu:
1. Bulgogi -- .00
2. Kalbi -- .00
3. Kimchi Fried Rice -- .00
Please choose one option at one time using the number (0 to end the order):
1
User Menu choice is 1
Thank you for ordering Bulgogi
Your total is
Here is our menu:
1. Bulgogi -- .00
2. Kalbi -- .00
3. Kimchi Fried Rice -- .00
Please choose one option at one time using the number (0 to end the order):
0
User Menu choice is 0
Your total after tax is .55
Here is our menu:
1. Bulgogi -- .00
2. Kalbi -- .00
3. Kimchi Fried Rice -- .00
Please choose one option at one time using the number (0 to end the order):
0
User Menu choice is 0
Your total after tax is .55
PS: 不要介意“您的税后总额是 XXXX”这一说法 - 它来自另一种方法。
如果我理解你上面的评论,请将你的主从更改为:
menu_received(); // First call
System.out.println("Value of menu_received : " + menu_received()); // Second call
tip_calculator(menu_received()); // Third call
收件人:
double result = menu_received(); // Only one call
System.out.println("Value of menu_received : " + result);
tip_calculator(result);
我正在使用以下代码制作以下关于在菜单上下订单的方法:
public static double menu_received() {
Scanner input = new Scanner(System.in);
int userResponse, total = 0;
do {
System.out.println();
System.out.println("Here is our menu:");
System.out.println("1. Bulgogi -- .00");
System.out.println("2. Kalbi -- .00");
System.out.println("3. Kimchi Fried Rice -- .00");
System.out.println("Please choose one option at one time using the number (0 to end the order):");
userResponse = input.nextInt();
System.out.println("User Menu choice is " + userResponse);
if (userResponse == 1) {
System.out.println("Thank you for ordering Bulgogi");
total = total + 15;
System.out.println("Your total is $" + total);
}
if (userResponse == 2) {
System.out.println("Thank you for ordering Kalbi");
total = total + 18;
System.out.println("Your total is $" + total);
}
if (userResponse == 3) {
System.out.println("Thank you for ordering Kimichi Fried Rice");
total = total + 16;
System.out.println("Your total is $" + total);
}
} while (userResponse != 0);
double total2 = total + 2.55;
// total = (double) total + 2.55;
System.out.println("Your total after tax is $" + total2);
return total2;
}
但是我遇到的问题是,当我调用它时,它没有按我想要的方式响应。即使在我输入 0 后它也不会停止调用菜单。 while(userResponse != 0) 我的情况是否有问题有人可以向我解释为什么以及如何纠正它吗?谢谢
这是我的输出:
>
Here is our menu:
1. Bulgogi -- .00
2. Kalbi -- .00
3. Kimchi Fried Rice -- .00
Please choose one option at one time using the number (0 to end the order):
1
User Menu choice is 1
Thank you for ordering Bulgogi
Your total is
Here is our menu:
1. Bulgogi -- .00
2. Kalbi -- .00
3. Kimchi Fried Rice -- .00
Please choose one option at one time using the number (0 to end the order):
1
User Menu choice is 1
Thank you for ordering Bulgogi
Your total is
Here is our menu:
1. Bulgogi -- .00
2. Kalbi -- .00
3. Kimchi Fried Rice -- .00
Please choose one option at one time using the number (0 to end the order):
0
User Menu choice is 0
Your total after tax is .55
Here is our menu:
1. Bulgogi -- .00
2. Kalbi -- .00
3. Kimchi Fried Rice -- .00
Please choose one option at one time using the number (0 to end the order):
0
User Menu choice is 0
Your total after tax is .55
PS: 不要介意“您的税后总额是 XXXX”这一说法 - 它来自另一种方法。
如果我理解你上面的评论,请将你的主从更改为:
menu_received(); // First call
System.out.println("Value of menu_received : " + menu_received()); // Second call
tip_calculator(menu_received()); // Third call
收件人:
double result = menu_received(); // Only one call
System.out.println("Value of menu_received : " + result);
tip_calculator(result);