我需要这个计算器来重定向用户输入错误的输入

时间:2015-06-25 15:15:09

标签: java switch-statement

在第三个输入中,它要求输入一个数字来执行特定操作。 但是,例如" 5"输入后,它不会自动将其发送到默认部分以显示消息"该值不存在。"。 除此之外,我还需要它再次显示输入提示并要求输入1到4之间的数字。 THX

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;
    }

}

4 个答案:

答案 0 :(得分:1)

通常情况下,您可以围绕"获取用户的输入"使用while循环,直到获得所需的输入。像这样。

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);
    }

答案 1 :(得分:1)

要重复输入,您可以执行以下操作:

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循环无论如何都会运行一次,然后检查最后的条件以查看它是否应该重复。

答案 2 :(得分:1)

我已经测试了您的代码, 打印&#34;价值不存在&#34;当输入5时。但是,你是正确的,因为它没有再问过它(因为你还没有使用过循环);

要连续要求用户输入一个数字,只要它们的数字不在1到4之间,你需要使用while循环。

这样的东西
while(opInt > 4 || opInt < 1){ //keep asking}

答案 3 :(得分:1)

使用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);
}