2017-08-31 105 views
0

我做了一個簡單的計算器程序,我得到這個異常:掃描儀:java.util.Scanner.next(未知來源)問題

java.util.InputMismatchException java.util.Scanner中.next(Unknown Source)

代碼運行的很好,但是當發生異常時,它不允許用戶使用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(); 

} 
} 
+0

你是什麼嘗試作爲你的輸入? – konsolas

+0

我試過了,它對我很有用 – azro

+0

這是因爲引發異常的'nextFoo()'方法不會消耗輸入,所以catch塊中的nextInt()調用在相同的錯誤輸入上窒息。有關更多詳細信息,請參閱https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo。 – azurefrog

回答

1

當你輸入一個錯誤的輸入,它會在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(); 
} 

而且,你有很多的代碼重複的,和變量名,這意味着什麼,這不是很好COMPAR e標準,我會建議這樣的事情來取代你的整個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); 
+0

azro - 非常感謝您的澄清。現在有道理。我已經閱讀了像這樣的問題在stackoverflow的15帖子,但沒有找到任何解釋,只有解決方案。感謝您澄清問題背後的原因及其解決方案:)非常感謝。 –

+0

@TarunjeetSinghSalh沒有問題;),並記住避免代碼複製,當你看到2-3行相同,尋找只得到一次這些;)也考慮投票/接受這個答案;) – azro

+0

感謝您的代碼提示格式並保持良好的標準。我已經投了票。我的名聲低於15,因爲我剛剛加入。我的投票被記錄但不可見。這將是如此,我將在從現在的Stackflow :)再次感謝好先生, –