java 执行方法后如何返回切换用例菜单?

w8f9ii69  于 2023-01-15  发布在  Java
关注(0)|答案(2)|浏览(188)

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

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,它出现在从扫描仪获取另一个输入的行上。

hgb9j2n6

hgb9j2n61#

问题是您使用的变量名为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();
}

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

yizd12fk

yizd12fk2#

这听起来好像可以用递归来解决。
我个人会实现这个类,(或集成一个新的方法):

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

这样可以确保每次选择后都返回到相同的状态,并将6作为退出条件。Main方法如下所示:

public static void main(String[] args) {

    Cmd.loop();

}

请记住,如果您不想实现额外的类,您可以简单地将loop方法添加到同一个类main is中。

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方法,而不是使用递归,将它包含在while(true)中(不太喜欢这个选项)

相关问题