我需要这个计算器来重定向用户输入,以防他输入错误

I need this calculator to redirect the user for input incase he enters the wrong one

在第三个输入中,它要求输入一个数字来进行特定的操作。 但是当输入例如“5”时,它不会自动将其发送到默认部分以显示消息 "the value doesn't exist."。 除此之外,我还需要它再次显示输入提示并要求输入从 1 到 4 的数字。谢谢

import java.io.BufferedReader;
import java.io.InputStreamReader;


public class Specialized {

    public static void main(String[] args) {
        String s1 = getInput("Enter a number: ");
        String s2 = getInput("Enter a number: ");
        String op = getInput("Enter: 1 = Add, 2 = Subtract, 3 = Multiply, 4    = Divide ");

         int opInt = Integer.parseInt(op);
         double result= 0;

        switch (opInt) {
        case 1:
            result = addValues(s1,s2);
            break;
        case 2:
            result = subtractValues(s1,s2);
            break;
        case 3:
            result = multiplyValues(s1,s2);
            break;
        case 4:
            result = divideValue(s1,s2);
            break;
        default:
            System.out.println("the value doesn't exist.");
            break;
        }
        System.out.println(result);
    }

    private static String getInput(String prompt) {
        BufferedReader stdin = new BufferedReader(
                new InputStreamReader(System.in));
        System.out.print(prompt);
        System.out.flush();

        try {
            return stdin.readLine();
        } catch (Exception e) {
            return "Error: " + e.getMessage();
        }
    }

    private static double addValues(String s1, String s2) 
         throws NumberFormatException {
        double d1 = Double.parseDouble(s1);
        double d2 = Double.parseDouble(s2);
        double result = d1 + d2;
        return result;
    }

    private static double subtractValues(String s1, String s2) {
        double d1 = Double.parseDouble(s1);
        double d2 = Double.parseDouble(s2);
        double result = d1 - d2;
        return result;
    }

    private static double multiplyValues(String s1, String s2) {
        double d1 = Double.parseDouble(s1);
        double d2 = Double.parseDouble(s2);
        double result = d1 * d2;
        return result;
    }

    private static double divideValue(String s1, String s2) {
        double d1 = Double.parseDouble(s1);
        double d2 = Double.parseDouble(s2);
        double result = d1 / d2;
        return result;
    }

}

通常,您用 while 循环包围 "getting of input from user",直到获得所需的输入。像这样。

public static void main(String[] args) {
        String s1 = getInput("Enter a number: ");
        String s2 = getInput("Enter a number: ");

        //notice I declare this outside now so I can compute for new value inside loop
        int opInt = 0;
        double result = 0;

        //you can change the condition later if you add more options
        while(opInt < 1 || opInt > 4) {
            String op = getInput("Enter: 1 = Add, 2 = Subtract, 3 = Multiply, 4    = Divide ");
            //recalculate the choice
            opInt = Integer.parseInt(op);

            switch (opInt) {
                case 1:
                    result = addValues(s1, s2);
                    break;
                case 2:
                    result = subtractValues(s1, s2);
                    break;
                case 3:
                    result = multiplyValues(s1, s2);
                    break;
                case 4:
                    result = divideValue(s1, s2);
                    break;
                default:
                    System.out.println("the value doesn't exist.");
                    break;
            }
        }
        System.out.println(result);
    }

要重复输入,您可以这样做:

double result= 0;
do {
     String op = getInput("Enter: 1 = Add, 2 = Subtract, 3 = Multiply, 4    = Divide ");

     int opInt = Integer.parseInt(op);

     boolean repeat = false;         

    switch (opInt) {
    case 1:
        result = addValues(s1,s2);
        break;
    case 2:
        result = subtractValues(s1,s2);
        break;
    case 3:
        result = multiplyValues(s1,s2);
        break;
    case 4:
        result = divideValue(s1,s2);
        break;
    default:
        System.out.println("the value doesn't exist.");
        repeat = true;
        break;
} while (repeat);

不管怎样,do while 循环都会运行一次,然后在最后检查一个条件,看它是否应该重复。

我测试了您的代码,当输入 5 时,它确实 打印 "the value doesn't exist"。但是,您是正确的,因为它不会再次询问(因为您没有使用循环);

要连续要求用户输入一个数字,只要他们的数字不在 1 到 4 之间(含 1 和 4),您需要使用 while 循环。

类似于

while(opInt > 4 || opInt < 1){ //keep asking}

使用 do-while 循环。这将再次提示用户并要求输入从 1 到 4 的数字:

public static void main(String[] args) {
    String s1 = getInput("Enter a number: ");
    String s2 = getInput("Enter a number: ");
    double result = 0;
    boolean repeat;

    do {
        String op = getInput("Enter > 1 = Add, 2 = Subtract, 3 = Multiply, 4 = Divide :");

        repeat = false;

        switch (Integer.parseInt(op)) {
        case 1:
            result = addValues(s1, s2);
            break;
        case 2:
            result = subtractValues(s1, s2);
            break;
        case 3:
            result = multiplyValues(s1, s2);
            break;
        case 4:
            result = divideValue(s1, s2);
            break;
        default:
            System.out.println("the value doesn't exist.");
            repeat = true;
            break;
        }

    } while (repeat);

    System.out.println(result);
}