简体   繁体   English

在具有开关情况的java中退出while循环?

[英]Exit while loop in java that has switch case?

I have a simple problem, I wrote switch case inside while loop that has 5 cases. 我有一个简单的问题,我在while循环内编写了5个案例的switch case。

My program should keep working all the time, but when i enter 5 the program should exit. 我的程序应该一直保持工作状态,但是当我输入5时,程序应该退出。

here is my code: 这是我的代码:

import java.util.Scanner;

public class Test3 {

public static void main(String argv[])
{
    Scanner input = new Scanner(System.in);
    System.out.println("1. Add Product");
    System.out.println("2. Edit Product");
    System.out.println("3. Delete Product");
    System.out.println("4. Search Product");
    System.out.println("5. Exit Application");
    int choice = input.nextInt();
    int i = 1;
    while(i == 1)
    {
    switch(choice)
    {
        case 1:
            System.out.println("Add");
            break;
        case 2:
            System.out.println("Edit");
            break;
        case 3:
            System.out.println("Delete");
            break;
        case 4:
            System.out.println("Search");
            break;
        case 5:
            i = 2;
            break;
        default:
            System.out.println("Invalid Choice .. Try Again.");
    }
    }
}

}

The problem is when i use one of the 4 cases. 问题是当我使用4种情况之一时。 for example if i enter 1 ... the program keep printing (Add) infinite ... i want to use case only one time then back again to the program to either enter another case or exit. 例如,如果我输入1 ...程序将无限打印(添加)...我只想使用一次,然后再次返回程序以输入另一种情况或退出。

You need to read input ( int choice = input.nextInt(); ) inside the while loop. 您需要在while循环中读取输入( int choice = input.nextInt(); )。 Otherwise choice never changes. 否则, choice永远不会改变。

int i = 1;
while(i == 1)
{
  int choice = input.nextInt();
  switch(choice)
  {
    case 1:
        System.out.println("Add");
        break;
    case 2:
        System.out.println("Edit");
        break;
    case 3:
        System.out.println("Delete");
        break;
    case 4:
        System.out.println("Search");
        break;
    case 5:
        i = 2;
        break;
    default:
        System.out.println("Invalid Choice .. Try Again.");
  }
}

Two alternatives to using a variable value for trigger end-of-loop. 使用可变值触发循环结束的两种选择。

Personally, I prefer method-style over both label-style and variable-style. 就个人而言,与标签样式和变量样式相比,我更喜欢方法样式。

// Using label
LOOP: for (;;) {
    switch (input.nextInt()) {
        case 1:
            System.out.println("Add");
            break; // exit switch, runs code after switch, then loops
        case 2:
            System.out.println("Add");
            continue LOOP; // loops immediately, skipping code after switch
        case 5:
            break LOOP; // exits loop, skipping code after switch
    }
    // Potential "code after switch" here
}

// Using return from method
private static void prompt() {
    for (;;) {
        switch (input.nextInt()) {
            case 1:
                System.out.println("Add");
                break;
            case 5:
                return; // we're done
        }
    }
}
boolean run = true;
while(run)
{
  int choice = input.nextInt();
  switch(choice)
  {
    case 1 :
        System.out.println("Add");
        break;
    case 2 :
        System.out.println("Edit");
        break;
    case 3 :
        System.out.println("Delete");
        break;
    case 4 :
        System.out.println("Search");
        break;
    case 5 :
        run = false;
        break;
    default:
        System.out.println("Invalid Choice .. Try Again.");
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM