異常問題,求幫忙
public int selectServer(){
int choose=0;
try{
choose = input.nextInt(); ?
if(choose<1||choose>6)
throw new Exception();
return choose;
} catch (InputMismatchException e){
System.out.println("輸入格式不正確,請重新正確數字:");
input.nextLine();
selectServer();
} catch (Exception e) {
System.out.println("請輸入正確數字(1~6):");
input.nextLine();
selectServer();
}
return choose;
}
第一次輸入錯誤拋出異常之后,重新輸入正確的數但是返回值不正確
2016-10-05
public static void selectServer(){
int choose=0;
input = new Scanner(System.in);
try{
choose = input.nextInt();?
if(choose<1||choose>6){
throw new Exception();
}
System.out.println("final =" + choose);
} catch (InputMismatchException e){
System.out.println("輸入格式不正確,請重新正確數字:");
input.nextLine();
selectServer();
} catch (Exception e) {
System.out.println("請輸入正確數字(1~6):");
input.nextLine();
selectServer();
}
2016-08-16
具體參考線的代碼
2016-08-05
因為你輸入錯誤數據的時候,直接去selectServer(),沒有 結束當前的線程,后面還有return choose,所有你第一次輸入的數據會在第二次打印出來
2016-08-05
這個函數,你每次出錯之后,都輸入一個數字,然后在重新執行selectServer(),又把choose置0 ,所以,,,,,你可以把那句話去掉試試
ps:新手,懂得不多。
2016-08-05
666