扫描程序:java.util.Scanner.next(未知来源)问题

时间:2017-08-31 16:54:26

标签: java

我已经制作了一个基本的计算器程序,我得到了这个例外:

  

java.util.InputMismatchException   java.util.Scanner.next(未知来源)

代码运行得很好,但是当发生异常时,它不允许用户使用Scanner输入。我做错了什么,我该如何解决?

package string;

import java.util.Scanner;
import java.lang.Exception;

public class Calculator {

double sum(double a,double b)
{
    double c =a+b;
    return c;
}

double subtract(double a,double b)
{
    double c= a-b;
    return c;
}

double multiply(double a,double b)
{
    double c=a*b;
    return c;
}

double divide(double a,double b)
{
    double c=a/b;
    return  c;
}


public static void main(String[] args) {
Calculator f= new Calculator();
int choice;
int z;

Scanner s1 =new Scanner(System.in);


do{
    try{

System.out.println("Welcome To Mini Calculator:  Which Function Do You Want To Use");
System.out.println("1.Addition");
System.out.println("2.Subtraction");
System.out.println("3.Multiplication");
System.out.println("4.Division");
System.out.println();
System.out.print("Please Enter Your Choice Number: ");
choice = s1.nextInt();

System.out.println();

switch(choice){
case 1: 
    System.out.print("Please Enter The First Number: ");
    double x= s1.nextDouble();
    System.out.println();
    System.out.print("Please Enter The Second Number: ");
    double y= s1.nextDouble();
    double u = f.sum(x,y);
    System.out.println();
    System.out.println("The Sum Of Two Numbers is: " + u);
    break;
case 2:
    System.out.print("Please Enter The First Number: ");
    double q= s1.nextDouble();
    System.out.println();
    System.out.print("Please Enter The Second Number: ");
    double w= s1.nextDouble();
    double i= f.subtract(q,w);
    System.out.println();
    System.out.println("The Substraction Of Two Numbers is: "+i );
    break;
case 3:
    System.out.print("Please Enter The First Number: ");
    double e= s1.nextDouble();
    System.out.println();
    System.out.print("Please Enter The Second Number: ");
    double r= s1.nextDouble();
    double o= f.multiply(e, r);
    System.out.println();
    System.out.println("The Multiplication Of Two Numbers " + o);
    break;
case 4:
    System.out.print("Please Enter The First Number: ");
    double t= s1.nextDouble();
    System.out.println();
    System.out.print("Please Enter The Second Number: ");
    double k= s1.nextDouble(); 
    double p= f.divide(t,k);
    System.out.println();
    System.out.println("The Divison of Two Numbers is: "+ p);
    break;
default:System.out.println(); 
    System.out.println("Please Enter a Valid Choice from 1 to 4");
}
}
catch(Exception e) {
    System.out.println("Input error: You have entered wrong input");
    System.out.println("Please restart the program");

    }
    System.out.println();
    System.out.println("Do You Want To perform Another Functionality?");
    System.out.println("Press 1 to Continue and Press 2 to Terminate The Program");
    z= s1.nextInt();  // Issue comes here. It runs fine without exception. When exception occurs in above code ,it doesn't take input and shows another exception
}
while(z==1);

System.out.println();
System.out.println("Thank You For Using Calculator");
s1.close();

}
}

1 个答案:

答案 0 :(得分:1)

当你输入错误的输入时,它会进入catch,但是输入仍然在这里,所以z= s1.nextInt();会抛出另一个未捕获的异常并且崩溃

所以你需要读取catch中的输入,以清除扫描器:

} catch (Exception e) {
    System.out.println("Input error: You have entered wrong input");
    System.out.println("Please restart the program");
    s1.nextLine();
}

此外,你有很多代码重复,变量名称没有任何意义,这与标准相比不是很好,我建议用这样的东西替换你的整个switch{ ... }

System.out.println();
System.out.print("Please Enter The First Number: ");
double numb1 = s1.nextDouble();
System.out.println();
System.out.print("Please Enter The Second Number: ");
double numb2 = s1.nextDouble();
double res;
String operation = "";

switch (choice) {
    case 1:
        res = f.sum(numb1, numb2);
        operation = "Sum";
        break;
    case 2:
        res = f.subtract(numb1, numb2);
        operation = "Substraction";
        break;
    case 3:
        res = f.multiply(numb1, numb2);
        operation = "Multiplication";
        break;
    case 4:
        res = f.divide(numb1, numb2);
        operation = "Divison";
        break;
    default:
        res = 0;
        System.out.println();
        System.out.println("Please Enter a Valid Choice from 1 to 4");
}

System.out.println();
System.out.println("The " + operation + " Of Two Numbers is: " + res);