Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在开关站中间终止while回路_Java_While Loop_Switch Statement - Fatal编程技术网

Java 在开关站中间终止while回路

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

当用户按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 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,为什么您认为当前代码应该退出?