簡體   English   中英

嵌套的do while循環從一個跳到另一個

[英]Nested do while loop jump from one to another

嘗試使用do-while循環時遇到一些邏輯困難。 在我的main()方法中。 我試圖一次又一次提示用戶輸入的大於6的值:

do{
System.out.println("select your option: ");
        System.out.println("1.option1");
        System.out.println("2.option2");
        System.out.println("3.option3");
        System.out.println("4.option4");
        System.out.println("5.option5");
        System.out.println("6.Quit");
        optionChoice = sc.nextInt();
        switch (optionChoice) {
        case 1:
            option1Method();
            break;
        } 
 } while (optionChoice > 6);

然后在我的option1Method() ,我還有另一個do while循環:

    do {
        System.out.println("select your option: ");
        System.out.println("1.opt1 method1");
        System.out.println("2.opt2 method2");
        System.out.println("3.opt3 method3");
        System.out.println("4.Back");
        optOption = sc.nextInt();
        switch (optOption ) {
        case 1: //do stuffs, same for case 2 and 3
           break;
        case 4: return;
        default: break;
       }
} while (optOption > 4);

對於此方法,我試圖一次又一次提示用戶選擇,只要他們輸入的值大於4。然后,當他們輸入4時,應返回main()方法中的do while循環。

但是,對於第二個do-while循環,當我輸入4時,程序本身剛剛終止。 有任何想法嗎?

提前致謝。

在主要方法中,將條件設置為:

optionChoice != 6

我不確定這是否是您想要的,但是我為您編寫了以下內容:

import java.util.Scanner;

public class Answer {
  static Scanner sc = new Scanner(System.in);
  static int optionChoice;

  public static void main(String[] args) {

    do{
      System.out.println("select your option: ");
      System.out.println("1.option1");
      System.out.println("2.option2");
      System.out.println("3.option3");
      System.out.println("4.option4");
      System.out.println("5.option5");
      System.out.println("6.Quit");
      optionChoice = sc.nextInt();

      switch (optionChoice) {
        case 1:
            option1Method();
          break;
      }
    } while (optionChoice > 6);
  }

  public static void option1Method() {

    int optOption;

    do {
       System.out.println("select your option: ");
       System.out.println("1.opt1 method1");
       System.out.println("2.opt2 method2");
       System.out.println("3.opt3 method3");
       System.out.println("4.Back");
       optOption = sc.nextInt();
       switch (optOption ) {
       case 1: //do stuffs, same for case 2 and 3
          break;
       case 4:
        optionChoice = 7; // you have to make this value greater than 6 if you want to continue in the loop
        return;
       default: break;
      }
    } while (optOption > 4);
  }
}

輸入4時的問題是返回主方法,而為optionChoice輸入的值是1,這會使while循環的條件為false

編輯:

為了回應@Timeout誰是完全正確的說法,我假設optionChoice是“全局變量”。

為了保持功能,我想您應該在main()方法的do-while循環中僅具有以下條件:

optionChoice > 6 || optionChoice == 1

編輯:

如果您在第二個while循環中添加為條件怎么辦

optOption != 4

這樣您就可以一直停留在循環中,直到用戶輸入4

EDIT TO HANDLE optionXMethod其中X是數字:

do{
      System.out.println("select your option: ");
      System.out.println("1.option1");
      System.out.println("2.option2");
      System.out.println("3.option3");
      System.out.println("4.option4");
      System.out.println("5.option5");
      System.out.println("6.Quit")
      optionChoice = sc.nextInt();

      switch (optionChoice) {
        case 1:
            option1Method();
            break;
        case 2:
            option2Method();
            break;
        case X:
            optionXMethod();
          break;
      }
    } while (optionChoice != 6);

void option1Method() {
    int optOption;

    do {
       System.out.println("select your option: ");
       System.out.println("1.opt1 method1");
       System.out.println("2.opt2 method2");
       System.out.println("3.opt3 method3");
       System.out.println("4.Back");
       optOption = sc.nextInt();
       switch (optOption ) {
       case 1: //do stuffs, same for case 2 and 3
          break;
// you do not need the case 4: because when optOptiontakes the value of 4 it leaves the loop

   default: break;
  }
} while (optOption != 4);

}

....

一般情況:

void optionXMethod() {
    int optOption;

    do {
       System.out.println("select your option: ");
       System.out.println("1.opt1 method1");
       System.out.println("2.opt2 method2");
       System.out.println("3.opt3 method3");
       System.out.println("4.opt4 method4");
       // more options
       System.out.println("X.Back"); // where X is the number option of Back
       optOption = sc.nextInt();
       switch (optOption ) {
       case 1: //do stuffs, same for case 2 and 3
          break;
// you do not need the case 4: because when optOptiontakes the value of 4 it leaves the loop

   default: break;
  }
} while (optOption != X); // whatever the value of X is should be consider for this condition

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM