0

我得到在編譯時出現以下錯誤:我要趕InputMismatchException時,但它顯示編譯時錯誤

filimon.java:12: error: cannot find symbol 
       }catch(InputMismatchException ime){ 
        ^
    symbol: class InputMismatchException 
    location: class filimon 
1 error 

我的源代碼是:

class filimon{ 
    public static void main(String[] args) { 
     Scanner s=new Scanner(System.in); 
     try{ 
      System.out.println("enter 2 integer values"); 
      int a=s.nextInt(); 
      int b=s.nextInt(); 
      System.out.println("value of a: "+a); 
      System.out.println("value of b: "+b); 
     }catch(InputMismatchException ime){ 
      System.err.println("please enter only number value"); 
     } 
     catch(Exception e){ 
      System.err.println(e); 
     } 
    }//main 
}//filimon 

問題是什麼?請幫幫我。

+0

什麼是您的進口? –

+0

毫無疑問,您還沒有確定編譯器包含哪些「InputMismatchException」類型。另外,你不處理這個異常,'catch(Exception e)'是一個反模式。請按照命名約定。 –

回答

0

Add import java.util.InputMismatchException;

0

Java無法找到InputMismatchException,因爲它沒有導入。

InputMismatchException是位於java.util

在你的文件的頂部,寫

import java.util.InputMismatchException; 

只導入異常,或

import java.util.*; 

java.util進口的一切。

一點題外話,在

catch(Exception e) 

是不是一個好主意。最好列出您想要捕捉的每個異常,無論是在其自己的catch塊中,還是如下所示:

catch(InputMismatchException|NoSuchElementException e){ 
相關問題