繁体   English   中英

方法执行后如何返回switch case菜单?

[英]How to return to switch case menu after executing method?

我目前正在编写一个程序,它可以从菜单中执行简单的算术函数。 理想情况下,我希望它执行从案例中选择的方法,然后在完成后返回到菜单。 相反,当它运行时,它只是在到达方法结束后结束程序。

public static void main(String[] args) {
        String menu = ("Please choose one option from the following menu: \n"
        + "1. Calculate the sum of integers 1 to m\n"
        + "2. Calculate the factorial of a given number\n"
        + "3. Calculate the amount of odd integers in a given sequence\n"
        + "4. Display the leftmost digit of a given number\n"
        + "5. Calculate the greatest common divisor of two given integers\n"
        + "6. Quit\n");
       
        Scanner console = new Scanner(System.in);
        do {

            System.out.println(menu);
            int selection = console.nextInt();

            switch (selection) {
                case 1:
                sumOfIntegers();
                break;

                case 2:
                factorialOfNumber();
                break;

                case 3:
                calcOddIntegers();
                break;

                case 4:
                displayLeftDigit();
                break;

                case 5:
                greatestCommonDivisor();
                break;

                case 6:
                System.out.println("Bye");
                break;
                
            }
        } while (selection != 6);
            console.close();
    }

我需要在选择菜单选项并完成方法后才能返回到开关盒菜单。 我已经尝试从另一个线程执行 do while 循环,但这对我不起作用。 我可能做错了什么并且对编码很陌生。 任何帮助表示赞赏。

编辑:我收到的错误是 NoSuchElementException 在它从扫描仪获取另一个输入的那一行。

问题是您使用的是在循环内定义的名为selection的变量,因此在循环外无法访问它。 要解决此问题,您应该在循环之前声明变量selection ,然后在循环条件内检查selection的值。

public static void main(String[] args) {
    String menu = ("Please choose one option from the following menu: \n"
    + "1. Calculate the sum of integers 1 to m\n"
    + "2. Calculate the factorial of a given number\n"
    + "3. Calculate the amount of odd integers in a given sequence\n"
    + "4. Display the leftmost digit of a given number\n"
    + "5. Calculate the greatest common divisor of two given integers\n"
    + "6. Quit\n");

    Scanner console = new Scanner(System.in);
    int selection = 0;
    do {
        System.out.println(menu);
        selection = console.nextInt();

        switch (selection) {
            case 1:
                sumOfIntegers();
                break;
            case 2:
                factorialOfNumber();
                break;
            case 3:
                calcOddIntegers();
                break;
            case 4:
                displayLeftDigit();
                break;
            case 5:
                greatestCommonDivisor();
                break;
            case 6:
                System.out.println("Bye");
                break;
        }
    } while (selection != 6);
    console.close();
}

通过此修改,程序将执行从案例中选择的方法,然后在完成后返回菜单。

这听起来好像可以用递归来解决。

我个人会实现这个 class,(或集成一个新方法):

    public class Cmd {

    private static final String menu = ("Please choose one option from the following menu: \n"
            + "1. Calculate the sum of integers 1 to m\n"
            + "2. Calculate the factorial of a given number\n"
            + "3. Calculate the amount of odd integers in a given sequence\n"
            + "4. Display the leftmost digit of a given number\n"
            + "5. Calculate the greatest common divisor of two given integers\n"
            + "6. Quit\n");

    private static final Scanner console = new Scanner(System.in);

    public static void loop() {

        System.out.println(menu);
        
        int selection = console.nextInt();



        switch (selection) {
            case 1:
                sumOfIntegers();
                break;
            case 2:
                factorialOfNumber();
                break;
            case 3:
                calcOddIntegers();
                break;
            case 4:
                displayLeftDigit();
                break;
            case 5:
                greatestCommonDivisor();
                break;
            case 6:
                System.out.println("Bye");
                console.close();
                return;
        }
        loop();
    }
}

通过这种方式,您可以确保在每次选择后返回到相同的 state,并将 6 作为退出条件。 主要方法如下所示:

public static void main(String[] args) {

    Cmd.loop();

}

请记住,如果您不想实现额外的 class,您可以简单地在相同的 class main 中添加循环方法。

public class wopa {

    private static final String menu = ("Please choose one option from the following menu: \n"
            + "1. Calculate the sum of integers 1 to m\n"
            + "2. Calculate the factorial of a given number\n"
            + "3. Calculate the amount of odd integers in a given sequence\n"
            + "4. Display the leftmost digit of a given number\n"
            + "5. Calculate the greatest common divisor of two given integers\n"
            + "6. Quit\n");

    private static final Scanner console = new Scanner(System.in);

    public static void main(String[] args) {
        loop();
    }

    public static void loop() {

        System.out.println(menu);

        int selection = console.nextInt();

        switch (selection) {
            case 1:
                sumOfIntegers();
                break;
            case 2:
                factorialOfNumber();
                break;
            case 3:
                calcOddIntegers();
                break;
            case 4:
                displayLeftDigit();
                break;
            case 5:
                greatestCommonDivisor();
                break;
            case 6:
                System.out.println("Bye");
                console.close();
                return;
        }
        loop();
    }
}

如果你想保持你拥有的相同模式。 您始终可以在 main 方法中添加 loop 方法,而不是使用递归 englobe it in a while(true) (不太喜欢这个选项)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM