键入p为double时出错?

时间:2014-03-09 21:16:27

标签: java

在提示操作员时将p键入扫描仪,例如1,2,3或4,我收到一条错误消息。其余代码通过,但我想知道如何将它输入到您可以输入的位置,例如,p,然后循环回到它,提示您输入正确的运算符

import java.util.Scanner;
public class charlespeppersprog5 {

    public static void main(String[] args)
    {
        Scanner operand = new Scanner (System.in);
        double leftOperand = 0;
        double rightOperand = 0;
        double operator = 0;
        double finalAnswer = 0;

        System.out.println("-----------------------------------------");
        System.out.println("");

        System.out.print("Enter the left operand : ");
        leftOperand = operand.nextDouble();
        System.out.print("Enter the right operand : ");
        rightOperand = operand.nextDouble();

        System.out.println("");
        System.out.println("-----------------------------------------");
        System.out.println("");

        System.out.println("\t 1 -> Multiplication");
        System.out.println("\t 2 -> Division");
        System.out.println("\t 3 -> Addition");
        System.out.println("\t 4 -> Subtraction");

        System.out.println("");
        System.out.println("-----------------------------------------");
        System.out.println("");

        System.out.print("Choose an operator from the above menu : ");
        operator = operand.nextDouble();
            while (operator!=1 && operator!=2 && operator!=3 && operator!=4)
            {
            System.out.print("Choose an operator from the above menu : ");
                operator = operand.nextDouble();
            }

enter code here //这里示例要求再次循环,如果在enter code here 1,2,3,4之外输入了任何内容。所以输入p你会收到这条消息 enter code here //线程“main”中的异常java.util.InputMismatchException     在java.util.Scanner.throwFor(未知来源)     在java.util.Scanner.next(未知来源)     在java.util.Scanner.nextDouble(未知来源)     在charlespeppersprog5.main(charlespeppersprog5.java:35)

        System.out.println("");
        System.out.println("-----------------------------------------");
        System.out.println("");

        if (operator==1)
        {
        finalAnswer = (leftOperand * rightOperand);
        System.out.println(rightOperand + " * " + leftOperand + " = " + finalAnswer);
        }

        if (operator==2)
        {
            finalAnswer = (leftOperand / rightOperand);
            System.out.println(rightOperand + " / " + leftOperand + " = " + finalAnswer);
        }

        if (operator==3)
        {
            finalAnswer = (leftOperand + rightOperand);
            System.out.println(rightOperand + " + " + leftOperand + " = " + finalAnswer);
        }

        if (operator==4)
        {
            finalAnswer = (leftOperand - rightOperand);
            System.out.println(rightOperand + " - " + leftOperand + " = " + finalAnswer);
        }

        System.out.println("");
        System.out.println("-----------------------------------------");

        operand.close();
    }
}

2 个答案:

答案 0 :(得分:1)

这是因为它期望一个双倍并得到别的东西。检查双重优先。

while (!operand.hasNextDouble()) {
    System.out.println("Please enter 1 - 4");
    operand.next(); // accept anything but exit while loop when conditions are met
} 
operator  = operand.nextDouble() ;

答案 1 :(得分:0)

您将收到一个错误,因为编译器正在考虑一个double值但是正在接收一个无效的参数,并且可能会抛出某种异常。您可以在try / catch块中包含要检查输入类型的代码,如下所示:

try {
    operator = operand.nextDouble();
} catch (InputMismatchException e) { // Note you are only catching invalid input. Other double values will still be accepted
    flag = false;
}
if (flag) {
    // do logic
} else {
    // go back
}

我希望你能够了解如何解决它。我只是为您展示了一个非常简单的逻辑 - 您可以根据需要实施它。