Java 在开关站中间终止while回路
当用户按2退出时,我想终止我的while循环,但如果用户按2退出,我又进入了循环,请帮助我,谢谢Java 在开关站中间终止while回路,java,while-loop,switch-statement,Java,While Loop,Switch Statement,当用户按2退出时,我想终止我的while循环,但如果用户按2退出,我又进入了循环,请帮助我,谢谢 int count = 1; while(count <=3) { System.out.println(" Enter any option"); System.out.println(" Enter 1 for addition"); System.out.println(" Enter 2 for exit"); Scanner input = new Scanne
int count = 1;
while(count <=3) {
System.out.println(" Enter any option");
System.out.println(" Enter 1 for addition");
System.out.println(" Enter 2 for exit");
Scanner input = new Scanner (System.in);
int option = input.nextInt();
if(option==2) {
System.out.println("Thank you for using app");
}
else
switch(option) {
case 1 :
System.out.println("welcome to addtion");
int sum;
System.out.println("Enter first number");
int x = input.nextInt();
System.out.println("Enter second number");
int y = input.nextInt();
sum=x+y;
System.out.println(sum);
break;
case 2 :
System.out.println("do you want to exit ? ");
break;
}
}
int count=1;
而(count也许循环的条件应该是:
while (option != 2)
您的当前状态-while(count您的while循环状态是可疑的,但是一行更改就足够了
if(option==2) {
System.out.println("Thank you for using app");
break;
}
你可以使用带标签的休息
input: while( count <=3) {
...
...
switch( option) {
...
case 2:
...
break input; // break the outer while loop
...
input:while(count)如果用户键入2,为什么您认为当前代码应该退出?