switch语句在java计算器中的连续性问题

mefy6pfw  于 2021-07-06  发布在  Java
关注(0)|答案(3)|浏览(211)

我是java新手,我正在做一个计算器应用程序。
我试图让我的计算器在“invalid input”后面的else语句之后不断提示用户输入正确答案(键入y或n)。
我希望程序在最终输入正确的输入后继续计算。
我玩过一个嵌入式游戏 while 循环,但最终得到一个无限循环或一个终止时没有分辨率的循环。代码如下。

import java.util.Scanner;
class Calculate {
    public static void.main(String[] args) {
            System.out.println("Welcome To Calculator!");
            System.out.println("*************************************************************************");
            Scanner userinput = new Scanner(System.in);
            double num1, num2;
            String choice;
            boolean youDecide = true;
            while(youDecide == true) {
                System.out.println("Please enter a number: ");
                num1 = userinput.nextDouble();
                System.out.println("Please enter an available operator (+, -, *, /): ");
                char operator = userinput.next().charAt(0);
                System.out.println("Please enter another number: ");
                num2 = userinput.nextDouble();
                double output;      
                switch(operator) 
                {
                case '+':
                    output = num1 + num2;
                    break;

                case '-':
                    output = num1 - num2;
                    break;

                case '*':
                    output = num1 * num2;
                    break;

                case '/':
                    output = num1 / num2;
                        if(num2 == 0)
                            System.out.println("Math error! A number cannot be divided by zero.");
                    break;
                default:
                    System.out.println("Invalid input. Please enter an available operator, i.e (+, -, *, /): ");
                    return;
                }
                System.out.println("*************************************************************************");
                System.out.println("The answer is: " + "\n" + output);
                System.out.println("*************************************************************************");
                System.out.println("Would you like to calculate again?");
                System.out.println("Please enter Y for yes, or N for no");
                choice = userinput.next();

                if(choice.equalsIgnoreCase("Y")) {
                    System.out.println("Okay. Let's continue!");
                    System.out.println("*************************************************************************");
                    youDecide = true;
                }
                else if(choice.equalsIgnoreCase("N")) {
                    System.out.println("*************************************************************************");
                    System.out.println("Okay. Thanks for using Calculator. Goodbye!");
                    System.exit(0);
                }
                else {
                    System.out.println("Invalid input. Please try again...");   
                    System.out.println("*************************************************************************");
                    youDecide = false;
                }
        }

    }

}
ubbxdtey

ubbxdtey1#

我对你的代码做了一些修改和注解

import java.util.Scanner;

class Calculator {
    public static void main(String[] args) {
        System.out.println("Welcome To Calculator!");
        System.out.println("*************************************************************************");
        Scanner userinput = new Scanner(System.in);
        double num1, num2;
        String choice;

        //the "youDecide" variable is not needed at all
        while (true) {
            System.out.println("Please enter a number: ");
            num1 = userinput.nextDouble();
            System.out.println("Please enter an available operator (+, -, *, /): ");
            char operator = userinput.next().charAt(0);
            System.out.println("Please enter another number: ");
            num2 = userinput.nextDouble();
            double output;
            switch (operator) {
            case '+':
                output = num1 + num2;
                break;

            case '-':
                output = num1 - num2;
                break;

            case '*':
                output = num1 * num2;
                break;

            case '/':
                output = num1 / num2;
                if (num2 == 0) {
                    System.out.println("Math error! A number cannot be divided by zero.");
                }
                break;
            default:
                System.out.println("Invalid input. Please enter an available operator, i.e (+, -, *, /): ");
                continue; //changed from "return" you don't want to exit, just to skip to the next execution of the loop
            }
            System.out.println("*************************************************************************");
            System.out.println("The answer is: " + "\n" + output);
            System.out.println("*************************************************************************");
            System.out.println("Would you like to calculate again?");
            System.out.println("Please enter Y for yes, or N for no");
            choice = userinput.next();

            if (choice.equalsIgnoreCase("Y")) {
                System.out.println("Okay. Let's continue!");
                System.out.println("*************************************************************************");
            } else if (choice.equalsIgnoreCase("N")) {
                System.out.println("*************************************************************************");
                System.out.println("Okay. Thanks for using Calculator. Goodbye!");
                System.exit(0);
            } else {
                System.out.println("Invalid input. Please try again...");
                System.out.println("*************************************************************************");
            }
        }

    }
}
kcugc4gi

kcugc4gi2#

你需要一个 while 循环以确保值正确。
由于代码中的逻辑,当用户键入char时需要执行此操作,否则必须更改许多行代码,或者再次请求所有值。
我认为对于乞丐来说,最简单的方法就是使用这个循环:

char operator;
while(true) {
    operator = userinput.next().charAt(0);
    if(operator=='+' || operator == '-' || operator == '*' || operator == '/') {
        break;
    }else {
        System.out.println("Please enter a valid operator: ");
    }
}

有很多方法可以做到这一点。但我认为对于乞丐来说,理解和实现代码是最简单的方法。
此循环仅用于确保用户键入有效字符。当字符不是其中之一时,循环将进行迭代。
您必须将此代码放置在要求有效运算符的此行下方。

f87krz0w

f87krz0w3#

在添加j.f.和javaman建议的编辑以及一些研究之后,我能够通过在代码中添加以下行来解决问题:

import java.util.InputMismatchException;
import java.util.Scanner;

class Calculator {
    public static void main(String[] args) {
        System.out.println("Welcome To Calculator!");
        System.out.println("*************************************************************************");
        Scanner userinput = new Scanner(System.in);
        char operator;
        double num1, num2;
        String choice;
        while(true) {
            System.out.println("Please enter a number: ");
            num1 = userinput.nextDouble();
            System.out.println("Please enter an available operator (+, -, *, /): ");
            while(true) {
                operator = userinput.next().charAt(0);  
                if(operator == '+' || operator == '-' || operator == '*' || operator == '/') {
                    break;
                }else {
                    System.out.println("Invalid input. Please enter an available operator, i.e (+, -, *, /): ");
                }
            }
            System.out.println("Please enter another number: ");
            num2 = userinput.nextDouble();
            double output;

            switch(operator) {
            case '+':
                output = num1 + num2;
                break;      
            case '-':
                output = num1 - num2;
                break;  
            case '*':
                output = num1 * num2;
                break;      
            case '/':
                output = num1 / num2;
                break;
            default:
                System.out.println("Invalid input. Please enter an available operator, i.e (+, -, *, /): ");
                continue;
            }
            System.out.println("*************************************************************************");                
            if(num2 == 0) {
                System.out.println("Math error! A number cannot be divided by zero.");
            }else {
                System.out.println("The answer is: " + "\n" + output);
            } 
            System.out.println("*************************************************************************");
            System.out.println("Would you like to calculate again?");
            System.out.println("Please enter Y for yes, or N for no");
            choice = userinput.next();

            if(choice.equalsIgnoreCase("Y")) {
                System.out.println("Okay. Let's continue!");
                System.out.println("*************************************************************************");
            }else if(choice.equalsIgnoreCase("N")) {
                System.out.println("*************************************************************************");
                System.out.println("Okay. Thanks for using Calculator. Goodbye!");
                System.exit(0);
            }else {
                System.out.println("Invalid input. Please try again...");   
                System.out.println("*************************************************************************");        
              **while (!("Y").equalsIgnoreCase(choice) && !("N").equalsIgnoreCase(choice)) {
                    System.out.println("Invalid input. Please try again...");   
                    System.out.println("*************************************************************************");        
                    System.out.println("Please enter Y for yes, or N for no");
                    choice = userinput.next();
                    if(choice.equalsIgnoreCase("Y")) {
                        System.out.println("Okay. Let's continue!");
                        System.out.println("*************************************************************************");
                    }else if(choice.equalsIgnoreCase("N")) {
                        System.out.println("*************************************************************************");
                        System.out.println("Okay. Thanks for using Calculator. Goodbye!");
                        System.exit(0);**
                    }
                }
            }
        }
   }

}

相关问题