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

如何在java中创建循环回菜单的方法,java,methods,Java,Methods,我正试图得到一个代码,它提供了几个选项供选择,虽然我可以让这些工作,但当我需要它返回到选项时,它会立即结束 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) pyth

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

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代码。它会无限地循环自身。

尝试使用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();
    }
}

要实现您希望执行的操作,最好的办法是使用do While循环。注意您请求输入的位置,然后如果选择与停止条件相等,则将其作为条件放在循环的while部分

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

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

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;
}

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

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

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。这是为了提高效率,所以如果它和第一个匹配,它就不会检查其余的匹配

你试着使用什么循环?您可以将其放入while循环,直到
choice!=5
。您还必须处理用户输入垃圾而不是1到5之间的数字的情况。您听说过
开关(选择){//…}
?@clinomaniac clino我试过了,但不起作用。它只会不断重复。@FredK这不是我的问题。现在我只想让代码工作,但它不工作。我得到的只是一个恒定的循环。它会重复菜单,除非我按5,我以为这是你要求的。如果没有,那么只需删除showMenu()调用并将break语句放在那里。很高兴提供帮助。如果这个或任何其他答案解决了您的问题,请接受它作为解决方案并投票表决。谢谢