如何在完成选择后重新启动菜单程序?

时间:2017-01-28 16:30:00

标签: java

选择完成后,我的程序陷入无限循环。它需要重新启动并再次浏览菜单选项。任何帮助将不胜感激。

public static void main(String[] args) {
    // TODO Auto-generated method stub
    @SuppressWarnings("resource")
    Scanner input = new Scanner(System.in);

    System.out.println("Menu");
    System.out.println("1. Add");
    System.out.println("2. Subtract");
    System.out.println("3. Multiply");
    System.out.println("4. Divide");
    System.out.println("5. Generate a random number");
    System.out.println("6. Quit\n");

    System.out.print("What would you like to do? ");
    int choice = input.nextInt();
    int count = 0;

    while (choice < 1 || choice > 6){
        count ++;
        System.out.println("I'm sorry, " +choice+ " is not a valid option.\n");
        System.out.println("Menu");
        System.out.println("1. Add");
        System.out.println("2. Subtract");
        System.out.println("3. Multiply");
        System.out.println("4. Divide");
        System.out.println("5. Generate a random number");
        System.out.println("6. Quit\n");
        System.out.print("What would you like to do? ");
        choice = input.nextInt();
        if (choice >= 1 && choice <= 6){
            continue;
        }
        else if (count == 2){
            System.out.println("Please try again later.");
            System.exit(0);
            return;
        }
      }


    do
    {
        switch (choice) 
        {
            case 1: choice = 1;

                System.out.print("What is the first number? ");
                int firstAdd = input.nextInt();
                System.out.print("What is the second number? ");
                int secondAdd = input.nextInt();
                System.out.println("Answer: " +(firstAdd + secondAdd));

                break;

            case 2: choice = 2;

                System.out.print("What is the first number? ");
                int firstSub = input.nextInt();
                System.out.print("What is the second nubmer? ");
                int secondSub = input.nextInt();
                System.out.println("Answer: " +(firstSub - secondSub));

                break;

            case 3: choice = 3;

                System.out.print("What is the first number?" );
                int firstMult = input.nextInt();
                System.out.print("What is the second number? ");
                int secondMult = input.nextInt();
                System.out.println("Answer: " +(firstMult * secondMult));

                break;

            case 4: choice = 4; 

                System.out.print("What is the first number? ");
                double firstDiv = input.nextInt();
                System.out.print("What is the second number? ");
                double secondDiv = input.nextInt();

                    while (secondDiv == 0){
                        System.out.println("I'm sorry, you can't divide by zero.");
                        break;}
                    {

                    System.out.println("Answer: " +(firstDiv / secondDiv));
                    break;
                    }


            case 5: choice = 5;

                System.out.println(Math.random() * 10 + 1);

                break;

            case 6: choice = 6;

                System.out.println("Goodbye!");

                System.exit(0);
                return;
        }

    }while (choice >=1 && choice <= 6);



}

我尝试了一些我在书中找到的不同选项,但它们似乎在其他方面造成了更多错误。我不知道是否有不同的声明而不是&#34; break;&#34;用来重启菜单,因为这是我第一次使用案例。

1 个答案:

答案 0 :(得分:1)

在while循环中将扫描仪实例化后的所有内容括起来。

具体地,

while (true) {
    // your code
    if (choice == 6)
        break;
}

FIXED:同时删除do {} while()循环。你的循环是无限的,因为“选择”不会在循环中改变。