為什么不出結(jié)果
package exercise2;
import java.util.InputMismatchException;
import java.util.Scanner;
public class test {
public static void main(String[] args) {
// TODO 自動生成的方法存根
? test ct=new test();
? ? try {
? ? ? ? ct.test();
? ? }catch(InputMismatchException e){
? ? System.out.println("請重新輸入");
? ? e.printStackTrace();
? ?
? ? }
? ??
? ??
}
public void test() {
Scanner input=new Scanner(System.in);
? ? String a=input.next();
}
}
2019-10-02
import java.util.InputMismatchException;
import java.util.Scanner;
public class Tct2 {
?public void test(){
??Scanner input = new Scanner(System.in);
??int a = input.nextInt();
?}
?public void show(){
??try{
???test();
??}catch(InputMismatchException e){
???System.out.println("類型異常,請重新輸入:");
???show();
??}
?}
?public static void main(String[] args) {
??Tct2 ojb = new Tct2();
??ojb.show();
?}
}
2019-03-03
因?yàn)槌绦驔]錯(cuò) 所以無法實(shí)現(xiàn)異常 所以。。
2018-07-29
因?yàn)槟爿斎氲亩紝儆谧址愋停]有捕捉到異常。不妨試試將最后一行代碼改成int a=input.nextInt();。然后輸入一個(gè)字符類型,這樣就能夠拋出類型不匹配異常,就能夠顯示catch后你想輸出的語句和異常信息。僅供參考。