簡體   English   中英

在具有開關情況的java中退出while循環?

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

我有一個簡單的問題,我在while循環內編寫了5個案例的switch case。

我的程序應該一直保持工作狀態,但是當我輸入5時,程序應該退出。

這是我的代碼:

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.");
    }
    }
}

}

問題是當我使用4種情況之一時。 例如,如果我輸入1 ...程序將無限打印(添加)...我只想使用一次,然后再次返回程序以輸入另一種情況或退出。

您需要在while循環中讀取輸入( int choice = input.nextInt(); )。 否則, 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.");
  }
}

使用可變值觸發循環結束的兩種選擇。

就個人而言,與標簽樣式和變量樣式相比,我更喜歡方法樣式。

// 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