有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何在java中创建循环回菜单的方法

我正试图得到一个代码,它提供了几个选项供选择,虽然我可以让这些工作,但当我需要它返回到选项时,它会立即结束

public static void main(String[] args) {
    System.out.println("Hello, This is the math program");
    System.out.println("Select an application from below: \n");
    System.out.println("(1) pythagoraen");
    System.out.println("(2) Subtraction");
    System.out.println("(3) Multiplication");
    System.out.println("(4) Division");
    System.out.println("(5) Exit");
    System.out.println("What is your Choice? ");

    Scanner input = new Scanner(System.in);
    int choice = input.nextInt();
    if (choice == 1) {
        pythagoraen();
        System.out.println(pythagoraen());
    } if (choice == 2) {
        subtraction();
    } if (choice == 3) {
        multiplication();
    } if (choice == 4) {
        division();
    } if (choice == 5){
        exitprogam();
    }
}
public static void subtraction(){
    Scanner input = new Scanner( System.in );
    int number1;
    int number2;
    int difference;

    System.out.print( "Enter First integer: " );
    number1 = input.nextInt();
    System.out.print( "Enter Second integer: ");
    number2 = input.nextInt();

    difference = number1 - number2;
    System.out.printf( "The difference is %d\n", difference);

}

我尝试了几个不同的while循环,试图让它返回选项,但它要么无休止地循环,要么不起作用

编辑:我添加了其中一个方法作为示例。案例建议不起作用,因为我认为格式太不一样了

public static void main(String[] args) {
    System.out.println("Hello, This is the math program");
    System.out.println("Select an application from below: \n");
    System.out.println("(1) pythagoraen");
    System.out.println("(2) Subtraction");
    System.out.println("(3) Multiplication");
    System.out.println("(4) Division");
    System.out.println("(5) Exit");
    System.out.println("What is your Choice? ");

    Scanner input = new Scanner(System.in);
    int choice = input.nextInt();
    while(choice!=5){
        if (choice == 1) {
            pythagoraen();
            System.out.println(pythagoraen());
        } if (choice == 2) {
            subtraction();
        } if (choice == 3) {
            multiplication();
        } if (choice == 4) {
            division();
        } if (choice == 5){
            exitprogam();
        }
}}

这是我唯一可以运行的while代码。它无限地自我循环


共 (4) 个答案

  1. # 1 楼答案

    实现这一点的方法有很多,但更好的方法是使用开关。使用switch语句,您可以有许多执行路径,这些路径似乎比使用while循环更适合这种情况。然而,while循环和switch语句实际上不能在效率方面进行比较,因为它们从根本上是不同的,如本article所示。以下是交换机的实现情况:

    public static void main(String[] args) {
        int exitFlag = 0;
        int choice = showMenu();
    
         do {
                switch (choice) {
                case 1:
                    System.out.println("pythagoraen()");
                    break;
                case 2:
                    System.out.println("subtraction()");
                    break;
                case 3:
                    System.out.println("multiplication()");
                    break;
                case 4:
                    System.out.println("division()");
                    break;
                case 5:
                    System.out.println("exitprogam()");
                    exitFlag = 1; 
                    break;
                default: 
                    System.out.println("Invalid Option()");
    
                    break;
                }
            if (exitFlag != 1) {
                choice = showMenu();    
            }
    
          }while (choice != 5);
    }
    
    private static int showMenu() {
        System.out.println("Hello, This is the math program");
        System.out.println("Select an application from below: \n");
        System.out.println("(1) pythagoraen");
        System.out.println("(2) Subtraction");
        System.out.println("(3) Multiplication");
        System.out.println("(4) Division");
        System.out.println("(5) Exit");
        System.out.println("What is your Choice? ");
    
        Scanner input = new Scanner(System.in);
        int choice = input.nextInt();
        return choice;
    }
    

    从长远来看,这对于重构来说更干净、更容易。有关交换机的更多信息,请参阅oracle docs

  2. # 2 楼答案

    你需要把你需要做的事情一次又一次地循环起来

    public static void main(String[] args) {
        System.out.println("Hello, This is the math program");
        System.out.println("Select an application from below: \n");
        System.out.println("(1) pythagoraen");
        System.out.println("(2) Subtraction");
        System.out.println("(3) Multiplication");
        System.out.println("(4) Division");
        System.out.println("(5) Exit");
        System.out.println("What is your Choice? ");
    
        Scanner input = new Scanner(System.in);
        int choice = input.nextInt();
        while(choice!=5){
            if (choice == 1) {
                pythagoraen();
            }
            else if (choice == 2) {
                subtraction();
            }
            else if (choice == 3) {
                multiplication();
            }
            else if (choice == 4) {
                division();
            }
            System.out.println("What is your next Choice? ");
            choice = input.nextInt();
        }
    }
    

    如果您需要再次打印菜单,那么您也可以将其放入循环中。在这里,你要做的是请求用户输入,然后选择你想要做的操作。最后,再次询问用户下一步要做什么。如果用户输入5,while条件变为false并退出循环

    我还删除了if choice == 5条件,因为while循环将处理该场景。在第一个if之后的每个if之前还添加了else。这是为了提高效率,所以如果它与第一个匹配,它不会检查其余的匹配项

  3. # 3 楼答案

    用while循环试试这个

    Scanner input = new Scanner(System.in);
    int choice = input.nextInt();
    while(choice != 5) {
    Scanner input = new Scanner(System.in);
    int choice = input.nextInt();
        if (choice == 1) {
            pythagoraen();
            System.out.println(pythagoraen());
        } if (choice == 2) {
            subtraction();
        } if (choice == 3) {
            multiplication();
        } if (choice == 4) {
            division();
        } if (choice == 5){
            exitprogam();
        }
    }
    
  4. # 4 楼答案

    要实现你想要做的事情,最好的办法是使用do While循环。注意你请求输入的位置,然后如果选择与你的停止条件相等,将其作为条件放在循环的while部分

    While循环将运行0-N次,而Do-While循环总是执行1-N次。利用这个优势

    这是一个很好的参考: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html