嵌套do while循环从一个跳转到另一个

时间:2016-03-19 02:29:57

标签: java loops do-while

尝试使用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()内,我有另一个while 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时,程序本身就被终止了。有什么想法吗?

提前致谢。

2 个答案:

答案 0 :(得分:2)

在main方法中,将条件设置为:

optionChoice != 6

答案 1 :(得分:0)

我不确定这是否是您想要的,但我已经为您写了以下内容:

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时的问题是你回到main方法,你为optionChoice输入的值是1,这使false成为while循环的条件。

修改

对@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

}