Reputation: 1
I want to loop a input (repeating) until enter 5 to exit. For example i input 1 display case 1, then ask input again , enter 4 and display case 4. Until i enter 5 to exit.
The below code just keep asking me input and never show me output.
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;
}
}
}
Upvotes: 0
Views: 5501
Reputation: 9971
Your switch
statement is outside the while-loop
scope.
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;
}
}
EDIT: Also, as others have pointed out in more recent answers, your loop condition variable isn't being reset inside the loop (but that's incidental to the question you asked and is, arguably, a different problem).
Upvotes: 1
Reputation: 69339
You need to make two fixes. Firstly, move your switch
block inside your while
loop. Secondly, your while loop will never terminate because you only assign input
once.
I can only imagine some of this confusion comes from having too many variables in your code: input
, input1
, check
...
It seems to me your code can be greatly simplified. Consider the following alternative - we can use a simple while(true)
loop, since you plan to exit the VM when 5
is entered:
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;
}
}
}
Upvotes: 0
Reputation: 11153
I would fix it like this:
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);
Adding a Do-While loop you guarantee it will show it at least once.
Or you can simply
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;
}
}
Upvotes: 0
Reputation: 2986
The switch
should be inside the while
. You must ask another input only after the first has been processed.
while (input is valid)
get input
process input
Upvotes: 1