如何制作一种循环回到Java菜单的方法



我正在尝试获取一个代码,该代码可以从中选择几个选项,虽然我可以让它们工作后立即结束,但我需要返回选项。<<<<<<<<<<<</p>

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 %dn", difference);
}

我尝试过几种不同的循环时,试图将其返回选项,但它要么无休止地循环或不起作用。

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

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

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

实现您想要做的事情的最佳选择是使用do-weel循环。请注意您要求输入的位置,然后选择等于您的停止条件,将其视为循环的一部分。

循环将运行0-N,而DO-while循环总是执行1-N。利用它为您的优势。

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

您需要将需要一遍又一遍地做的事情放在循环中。

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循环将处理该方案。如果在第一个之后,则在每个之前都会添加其他。这是为了效率,因此如果与第一场比赛匹配,则不会检查其余的比赛。

有很多方法可以实现此目的,但是更好的方法是使用开关。使用Switch语句,您可以拥有许多执行路径,这些执行路径似乎比使用WALE循环更适合这种情况。但是,正如本文所示,一定的循环和开关语句无法真正通过效率进行比较,因为它们在根本上是不同的。这是交换机的实现方式:

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文档。

尝试使用while loop

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

最新更新