我怎样才能return到主菜单?

How can I return to the main menu?

我有一个项目,如果从第二个菜单中选择它,我希望它返回到第一个菜单。

以下仅为示例:

import java.util.Scanner;

public class SAMPLE {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

这是第一个菜单:

    System.out.println("Enter choice: ");
    System.out.println("1 = A");
    System.out.println("2 = B");
    int choice1 = input.nextInt();

    do {

这是第二个菜单:

        System.out.println("Another choice:");
        System.out.println("1 = C");
        System.out.println("2 = D");
        System.out.println("3 = Go back");
        int choice2 = input.nextInt();

        switch (choice2) {
        case 1: System.out.println("nice");
            break;
        case 2: System.out.println("nicee");
            break;

如果我选择了3,它应该会回到第一个菜单,但我不知道如何。

        case 3: System.out.println("How can I go back to the first menu? HELP!");
            break;
        }

    } while (choice1 > 2);

    System.out.println("END OF THE PROGRAM");
}
}

我建议将您的代码拆分为函数。

对于情况 3 - 您应调用您的新函数。

像这样:

static int mainMenu(Scanner inputScanner)
{
System.out.println("Enter choice: ");
System.out.println("1 = A");
System.out.println("2 = B");
return inputScanner.nextInt(); //Assume valid input. you should add a valid check
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

int Choice1  = mainMenu(input);
do { 

    System.out.println("Another choice:");
    System.out.println("1 = C");
    System.out.println("2 = D");
    System.out.println("3 = Go back");
    int Choice2 = input.nextInt();

    switch (Choice2) {
    case 1: //DO Y
        break;
    case 2: //DO X
        break; 
    case 3: Choice1 = mainMenu(input);
        break;
    }

} while (Choice1 > 2); 
//END logic
}

啊哈你在选择C和D的过程中想回到选择A和B的第一个菜单!

有很多种方法(比如使用其他方法或递归), 但我推荐这种方法,现在更容易理解。

首先,让我们检查一下代码来组织我们要做的事情 <我稍微改了一下,让它更容易辨认!对不起!>

import java.util.Scanner;

public class StackOver {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Scanner input = new Scanner(System.in);

  ////main_menu///-Select A or B
  System.out.println("Please input Enter Choice: ");
  System.out.println("input '1' to Select A");
  System.out.println("input '2' to Select B");
  int choice1 = input.nextInt();
  ////////////////////////////


  ////second_Menu Choose C or D, or Back to main_menu
  System.out.println("Another choice: ");
  System.out.println("input '1' to Select C");
  System.out.println("input '2' to Select D");
  System.out.println("input '3' to Go back");
  int choice2 = input.nextInt();


  //According to 'choice2'
  switch(choice2) {
  case 1: System.out.println("nice, you Select C");
     break;
  case 2: System.out.println("nicee, you Select D");
     break;

  case 3: System.out.println("Now, you can go to Back");
     break;
  }
  //I'm sorry, but I can't understand the meaning of 
  //'while (choice1 > 2);' ㅠㅅㅠ

  System.out.println("END OF THE PROGRAM");
  }

}

所以上面的代码就是你要的,对吧?

有很多方法可以按照你想要的方向实现。

让我们尝试通过您尝试使用的 While 语句将其返回到主菜单。

代码进行中。

  1. (1)选择A和B
  2. (2) 选择 C ​​和 D 或 (1)

它包括转到 (1) 或结束程序,具体取决于 (2) 的结果。

使用 do{ }while(boolean); 怎么样? do{} 中的过程将一直持续到 'boolean' 为 false。

现在我们可以这样修改代码

import java.util.Scanner;

public class StackOver {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner input = new Scanner(System.in);
    int choice2 = 0; //init for NullPointerError


    do {
    //below do{
    ////main_menu///-Select A or B
        System.out.println("Please input Enter Choice: ");
        System.out.println("input '1' to Select A");
        System.out.println("input '2' to Select B");
        int choice1 = input.nextInt();
        ////////////////////////////


        ////second_Menu Choose C or D, or Back to main_menu
        System.out.println("Another choice: ");
        System.out.println("input '1' to Select C");
        System.out.println("input '2' to Select D");
        System.out.println("input '3' to Go back");
        choice2 = input.nextInt();


        //According to 'choice2'
        switch(choice2) {
        case 1: System.out.println("nice, you Select C");
            break;
        case 2: System.out.println("nicee, you Select D");
            break;

        case 3: System.out.println("Now, you can go to Back");
            break;
        }

    }while(choice2 == 3);
    //if choice2 == 3, then the progress of code return back to below 'do{'

    System.out.println("END OF THE PROGRAM");
    }

}

这是更改代码后的结果。

希望大家看得懂,有什么不明白的地方欢迎留言! 希望你今天过得愉快祥和。