InputMismatchException cannot be resolved to a type
try{ System.out.println("輸入第一個數"); Scanner?input?=?new?Scanner(System.in); int?firstNum?=?input.nextInt(); System.out.println("輸入第二個數"); int?secondNum?=?input.nextInt(); int?result?=?firstNum/secondNum; System.out.println("兩數之商為"?+?result); }?catch?(ArithmeticException?e)?{ System.out.println("除數為0"); }?catch?(InputMismatchException?e){ System.out.println("需要輸入整數"); }?catch?(Exception?e){ System.out.println("不知名錯誤"); }
剛開始提示“InputMismatchException cannot be resolved to a type”,后來在前面import 了 java.util.InputMismatchException之后就OK了,但是AritheticException 這個異常就不需要import什么類啊,不明白是怎么回事~~求指導?
? java.lang.RuntimeException
? ? ? ? ? ? ?java.util.NoSuchElementException
? ? ? ? ? ? ? ? ?java.util.InputMismatchException
java.lang.RuntimeException
? ? ? ? ? ? ?java.lang.ArithmeticException
2016-05-25
在前面加入import java.util.*;就可以了
2015-12-15
因為ArithmeticException這個類是存在于java.lang這個包的,默認情況下這個包中的所有類會被自動導入所有的程序。而InputMismatchException是java.util的包,需要手動導入