Java 计算器中的连续性问题

Continuity Issue in Java Calculator

我是新手Java,正在做一个计算器APP

我正在尝试让我的计算器在 '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;
                }
        }
    
    }

}

您需要一个 while 循环来确保值正确。

由于代码中的逻辑,您需要在用户键入字符时执行此操作,否则您必须更改多行代码,或者再次询问所有值。

我认为对于初学者来说最简单的方法是使用这个循环:

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

有很多更优雅的方法可以做到这一点。但我认为对于初学者来说,理解和实现代码是最简单的方法。

这个循环只是为了确保用户键入一个有效的字符。虽然角色不是其中之一,但循环将迭代。

您必须将此代码放在要求有效运算符的这一行下方。

我对你的代码做了一些修改和评论

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

    }
}

添加 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);**
                    }
                }
            }
        }
   }

}