繁体   English   中英

扫描仪:java.util.Scanner.next(未知源)问题

[英]Scanner : java.util.Scanner.next(Unknown Source) issue

我编写了一个基本的计算器程序,但遇到了以下异常:

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

}
}

当您输入错误的输入时,它会进入catch但输入仍在此处,因此z= s1.nextInt(); 引发另一个未被捕获的异常并崩溃

因此,您需要阅读捕获中的输入,以清除扫描仪:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM