Scanner:java.util.Scanner.next(未知源)问题
我制作了一个基本的计算器程序,我得到了一个例外: java.util.InputMismatchException java.util.Scanner.next(未知源) 代码运行正常,但当出现异常时,它不允许用户使用Scanner:java.util.Scanner.next(未知源)问题,java,Java,我制作了一个基本的计算器程序,我得到了一个例外: java.util.InputMismatchException java.util.Scanner.next(未知源) 代码运行正常,但当出现异常时,它不允许用户使用Scanner进行输入。我做错了什么?我该如何修复它 package string; import java.util.Scanner; import java.lang.Exception; public class Calculator { double sum(doub
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中的输入,以清除扫描仪:
} catch (Exception e) {
System.out.println("Input error: You have entered wrong input");
System.out.println("Please restart the program");
s1.nextLine();
}
另外,你有很多代码重复,变量名没有任何意义,这与标准相比不是很好,我建议用这样的东西来代替你的整个开关{…}
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);
您尝试了什么作为输入?我尝试了,它对我有效,因为引发异常的nextFoo()
方法没有使用输入,所以catch块中的nextInt()
调用被相同的错误输入阻塞。有关更多详细信息,请参阅。另外,一般情况下,不要对流控制使用异常,而是在尝试阅读之前使用hasnetfoo()
检查有效输入。azro-非常感谢您的澄清。现在有道理了。我在stackoverflow上读了15篇关于这类问题的帖子,但没有找到任何解释,只有解决方案。感谢您澄清问题背后的原因及其解决方案:)非常感谢。@Tarunjeethinghsalh没问题;)记住要避免代码重复,当您看到2-3行相同时,请只查找一次get;)同时考虑投票赞成/接受这个答案;)感谢您提供有关代码格式和维护良好标准的提示。我投了赞成票。我的名声不到15岁,因为我刚刚加入。我的投票记录在案,但不可见。从现在起,我将继续使用stackflow:)再次感谢好先生,@TarunjeetSinghSalh您可以接受答案,这比投票赞成要好;)