微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如果多次选择无效的菜单项,我正在尝试弹出一条消息

如何解决如果多次选择无效的菜单项,我正在尝试弹出一条消息

目标是如果用户在 3 次中输入无效选项,程序将告诉用户稍后再试并结束程序。我的代码在下面,我希望这是有道理的,如果没有,我很抱歉。如果您有任何问题,请告诉我。我也从来没有在这里问过一个问题,所以它可能没有正确上传

public static void main(String[] args) {
    int num1 = 0,num2 = 0,total = 0,option = 0,ex; // Creates integer variables
    do {
        Scanner sc = new Scanner(system.in);
        System.out.println("\tBasic Math Calculator");// Title
        System.out.println("\t---------------------");
        System.out.println("\tEnter your choice from the following menu:");
        System.out.println("\t------------------------------------------");
        System.out.println("1.\tAddition");// All the menu options
        System.out.println("2.\tSubtraction");
        System.out.println("3.\tMultiplication");
        System.out.println("4.\tDivision");
        System.out.println("5.\tGenerate Random number");
        System.out.println("6.\tQuit");

        boolean valid;
        do {
            valid = true;
            try {
                option = Integer.parseInt(sc.nextLine());// Stores the users answers
                if (option < 1 || option > 6) {
                    System.out.println("Invalid input. Please try again.");
                    valid = false;
                }
            } catch (NumberFormatException e) {
                System.out.println("Invalid input. Please try again.");
                valid = false;
            }
        } while (!valid);

        switch (option) {// The math and titles for every option
        case 1:
            System.out.println("You chose to add two numbers: ");
            System.out.println("Enter your first number:");
            num1 = sc.nextInt();
            System.out.println("Enter your second number:");
            num2 = sc.nextInt();
            total = num1 + num2;
            System.out.println("The two numbers you chose added together is " + total);
            break;
        case 2:
            System.out.println("You chose to subtract two numbers: ");
            System.out.println("Enter your first number:");
            num1 = sc.nextInt();
            System.out.println("Enter your second number:");
            num2 = sc.nextInt();
            total = num1 - num2;
            System.out.println("The two numbers you chose subtracted together is " + total);
            break;
        case 3:
            System.out.println("You chose to multiply two numbers: ");
            System.out.println("Enter your first number:");
            num1 = sc.nextInt();
            System.out.println("Enter your second number:");
            num2 = sc.nextInt();
            total = num1 * num2;
            System.out.println("The two numbers you chose multiplied together is " + total);
            break;
        case 4:
            System.out.println("You chose to divide two numbers: ");
            System.out.println("Enter your first number:");
            num1 = sc.nextInt();
            System.out.println("Enter your second number:");
            num2 = sc.nextInt();
            total = num1 / num2;
            if (num2 == 0) {
                System.out.println("You can't divide by 0");
            } else {
                System.out.println("The two numbers you chose divided together is " + total + "with a quotient of "
                        + (num1 % num2));
            }
            break;
        case 5:
            System.out.println("You chose to get two random numbers: ");
            System.out.println("Enter your lower limit:");
            num1 = sc.nextInt();
            System.out.println("Enter your upper limit:");
            num2 = sc.nextInt();
            total = num1 + num2;
            Random rand = new Random();
            int rand_int1 = rand.nextInt(num1 + num2);
            System.out.println("The random intigers is: " + rand_int1);
            break;
        case 6:// If the user wants to quit
            ex = 2;
            break;
        default:// Tells their option was incorrect
            System.out.println("Invalid choice,choice " + option + " was not an option");

        }
        System.out.println("Do you want to continue?1.Yes 2.No");// Asks the user if they want to proceed
        ex = sc.nextInt(); // A thank you message for the user for running the program
    } while (ex == 1);
    System.out.println("-----------------------------------------");
    System.out.println("Thank you for using the basic calculator!");
}

解决方法

您可以通过在 start if switch 语句之前创建一个嵌套循环来实现:

//...

System.out.println("6.\tQuit");

boolean valid;
do {
    valid = true;
    try {
        option = Integer.parseInt(sc.nextLine());// Stores the users answers
        if (option < 1 || option > 6) {
            System.out.println("Invalid input. Please try again.");
            valid = false;
        }
    } catch (NumberFormatException e) {
        System.out.println("Invalid input. Please try again.");
        valid = false;
    }
} while (!valid);

switch (option) {// The math and titles for every option

//...

此更改后的示例运行:

    Basic Math Calculator
    ---------------------
    Enter your choice from the following menu:
    ------------------------------------------
1.  Addition
2.  Subtraction
3.  Multiplication
4.  Division
5.  Generate Random number
6.  Quit
a
Invalid input. Please try again.
9
Invalid input. Please try again.
4
You chose to divide two numbers: 
Enter your first number:

ideone 查看完整代码。

,

您不必使用 echo "<a href='search.php?food=$i'>$prod_ar[$i]</a>"."</br>";valid 来检查无效输入的次数

如果您想在用户输入 3 个无效选项后退出程序,您必须增加 do-while 的计数并更改 ex 循环中的条件。

初始化while

更改 case 6 和默认如下

ex=1

while 条件如下

case 6:// If the user wants to quit
            System.exit(0);
            break;
default:
         ex = ex+1;
         break; 

以下是完整代码

while (ex <4);  // 

已编辑 下面是示例输出


public static void main(String[] args) {
    int num1 = 0,num2 = 0,total = 0,ex= 1; // Creates integer variables
    do {
        Scanner sc = new Scanner(System.in);
        System.out.println("\tBasic Math Calculator");// Title
        System.out.println("\t---------------------");
        System.out.println("\tEnter your choice from the following menu:");
        System.out.println("\t------------------------------------------");
        System.out.println("1.\tAddition");// All the menu options
        System.out.println("2.\tSubtraction");
        System.out.println("3.\tMultiplication");
        System.out.println("4.\tDivision");
        System.out.println("5.\tGenerate Random number");
        System.out.println("6.\tQuit");



       switch (option) {// The math and titles for every option
        case 1:
            System.out.println("You chose to add two numbers: ");
            System.out.println("Enter your first number:");
            num1 = sc.nextInt();
            System.out.println("Enter your second number:");
            num2 = sc.nextInt();
            total = num1 + num2;
            System.out.println("The two numbers you chose added together is " + total);
            break;
        case 2:
            System.out.println("You chose to subtract two numbers: ");
            System.out.println("Enter your first number:");
            num1 = sc.nextInt();
            System.out.println("Enter your second number:");
            num2 = sc.nextInt();
            total = num1 - num2;
            System.out.println("The two numbers you chose subtracted together is " + total);
            break;
        case 3:
            System.out.println("You chose to multiply two numbers: ");
            System.out.println("Enter your first number:");
            num1 = sc.nextInt();
            System.out.println("Enter your second number:");
            num2 = sc.nextInt();
            total = num1 * num2;
            System.out.println("The two numbers you chose multiplied together is " + total);
            break;
        case 4:
            System.out.println("You chose to divide two numbers: ");
            System.out.println("Enter your first number:");
            num1 = sc.nextInt();
            System.out.println("Enter your second number:");
            num2 = sc.nextInt();
            total = num1 / num2;
            if (num2 == 0) {
                System.out.println("You can't divide by 0");
            } else {
                System.out.println("The two numbers you chose divided together is " + total + "with a quotient of "
                        + (num1 % num2));
            }
            break;
        case 5:
            System.out.println("You chose to get two random numbers: ");
            System.out.println("Enter your lower limit:");
            num1 = sc.nextInt();
            System.out.println("Enter your upper limit:");
            num2 = sc.nextInt();
            total = num1 + num2;
            Random rand = new Random();
            int rand_int1 = rand.nextInt(num1 + num2);
            System.out.println("The random intigers is: " + rand_int1);
            break;
        case 6:// If the user wants to quit
            System.out.println("Thank you for using the basic calculator!");            
            System.exit(0);
            break;
        default:// Tells their option was incorrect
            ex=ex+1;
            System.out.println("Invalid choice,choice " + option + " was not an option");

        }
        System.out.println("Do you want to continue?1.Yes 2.No");// Asks the user if they want to proceed
        //ex = sc.nextInt(); // A thank you message for the user for running the program
    } while (ex<4);
    System.out.println("-----------------------------------------");
    System.out.println("Thank you for using the basic calculator!");
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。