我想循环输入(重复),直到输入5退出。例如,我输入1个展示柜1,然后再次询问输入,输入4并显示案例4.直到我输入5退出。
下面的代码只是一直问我输入,从不向我显示输出。
public static void main(String[] args){
System.out.println("1.Soup\n2.Main Course\n3.Dessert\n4.Beverage\n5.Exit");
//ask input from user
System.out.println("Enter your choice: ");
Scanner scan=new Scanner(System.in);
int input=scan.nextInt();
int check=input;
//keep looping until user input 5 which is Exit
while (input!=5){
System.out.println("Please enter 1-5\n1.Soup\n2.Main Course\n3.Dessert\n4.Beverage\n5.Exit");
int input1=scan.nextInt();
check=input1;
}
switch (check){
case 1 :
System.out.println("1.Mushroom Soup\n2.Miso Soup\n3.Tomato Soup");
break;
case 2 :
System.out.println("1.Chiken Katsu Don\n2.Curry Katsu Don\n3.Teriyaki Katsu Don\n4.Curry Udon");
break;
case 3 :
System.out.println("1.Matcha Ice Cream\n2.Chocolate Ice Cream\n3.Oreo Chocolate\n4.Dango");
break;
case 4 :
System.out.println( "1.Green Tea\n2.Matcha RedBean\n3.Chocolate MilkShake\n4.Strawberry Ice Blended");
break;
case 5 :
System.exit(0);
break;
}
}
}
答案 0 :(得分:1)
switch
应位于while
内。只有在处理完第一个输入后才能询问另一个输入。
while (input is valid)
get input
process input
答案 1 :(得分:1)
您的switch
声明超出while-loop
范围。
while (input!=5)
{
System.out.println("Please enter 1-5\n1.Soup\n2.Main Course\n3.Dessert\n4.Beverage\n5.Exit");
int input1=scan.nextInt();
check=input1;
switch (check)
{
case 1 :
System.out.println("1.Mushroom Soup\n2.Miso Soup\n3.Tomato Soup");
break;
case 2 :
System.out.println("1.Chiken Katsu Don\n2.Curry Katsu Don\n3.Teriyaki Katsu Don\n4.Curry Udon");
break;
case 3 :
System.out.println("1.Matcha Ice Cream\n2.Chocolate Ice Cream\n3.Oreo Chocolate\n4.Dango");
break;
case 4 :
System.out.println( "1.Green Tea\n2.Matcha RedBean\n3.Chocolate MilkShake\n4.Strawberry Ice Blended");
break;
case 5 :
System.exit(0);
break;
}
}
编辑:另外,正如其他人在最近的答案中指出的那样,你的循环条件变量没有在循环内重置(但这是你提出的问题的偶然问题,可以说是一个不同的问题)。 p>
答案 2 :(得分:0)
我会这样修理:
do{
System.out.println("Please enter 1-5\n1.Soup\n2.Main Course\n3.Dessert\n4.Beverage\n5.Exit");
int input1=scan.nextInt();
check=input1;
switch (check){
case 1 :
System.out.println("1.Mushroom Soup\n2.Miso Soup\n3.Tomato Soup");
break;
case 2 :
System.out.println("1.Chiken Katsu Don\n2.Curry Katsu Don\n3.Teriyaki Katsu Don\n4.Curry Udon");
break;
case 3 :
System.out.println("1.Matcha Ice Cream\n2.Chocolate Ice Cream\n3.Oreo Chocolate\n4.Dango");
break;
case 4 :
System.out.println( "1.Green Tea\n2.Matcha RedBean\n3.Chocolate MilkShake\n4.Strawberry Ice Blended");
break;
case 5 :
System.exit(0);
break;
}
}
while (input!=5);
添加Do-While循环可确保它至少显示一次。
或者你可以简单地
while (input!=5){
System.out.println("Please enter 1-5\n1.Soup\n2.Main Course\n3.Dessert\n4.Beverage\n5.Exit");
int input1=scan.nextInt();
check=input1;
switch (check){
case 1 :
System.out.println("1.Mushroom Soup\n2.Miso Soup\n3.Tomato Soup");
break;
case 2 :
System.out.println("1.Chiken Katsu Don\n2.Curry Katsu Don\n3.Teriyaki Katsu Don\n4.Curry Udon");
break;
case 3 :
System.out.println("1.Matcha Ice Cream\n2.Chocolate Ice Cream\n3.Oreo Chocolate\n4.Dango");
break;
case 4 :
System.out.println( "1.Green Tea\n2.Matcha RedBean\n3.Chocolate MilkShake\n4.Strawberry Ice Blended");
break;
case 5 :
System.exit(0);
break;
}
}
答案 3 :(得分:0)
您需要进行两次修复。首先,将switch
块移到while
循环中。其次,你的while循环永远不会终止,因为你只分配input
一次。
我只能想象一些混淆来自代码中包含太多变量:input
,input1
,check
......
在我看来,您的代码可以大大简化。考虑以下替代方案 - 我们可以使用简单的while(true)
循环,因为您计划在输入5
时退出VM:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (true) {
System.out.println("Please enter 1-5\n1.Soup\n2.Main Course\n3.Dessert\n4.Beverage\n5.Exit");
int input = scan.nextInt();
switch (input) {
case 1:
System.out.println("1.Mushroom Soup\n2.Miso Soup\n3.Tomato Soup");
break;
case 2:
System.out.println("1.Chiken Katsu Don\n2.Curry Katsu Don\n3.Teriyaki Katsu Don\n4.Curry Udon");
break;
case 3:
System.out.println("1.Matcha Ice Cream\n2.Chocolate Ice Cream\n3.Oreo Chocolate\n4.Dango");
break;
case 4:
System.out.println("1.Green Tea\n2.Matcha RedBean\n3.Chocolate MilkShake\n4.Strawberry Ice Blended");
break;
case 5:
System.exit(0);
break;
}
}
}