這是我迄今爲止寫的內容,但是當引發異常時,它不會再要求用戶輸入。如何從鍵盤取得整數輸入,如果輸入無效,我該如何再次詢問用戶?
do {
System.out.println("Enter the number of stones to play with: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String temp = br.readLine();
key = Integer.parseInt(temp);
} while (key < 0 && key > 9);
if (key < 0 || key > 10)
throw new InvalidStartingStonesException(key);
player1 = new KeyBoardPlayer();
player2 = new KeyBoardPlayer();
this.player1 = player1;
this.player2 = player2;
state = new KalaGameState(key);
} catch (NumberFormatException nFE) {
System.out.println("Not an Integer");
} catch (IOException e) {
System.out.println(e);
}
的代碼確實沒有做到這一點。您之前已經使用另一個帳戶問過類似問題:http://stackoverflow.com/questions/2614930/i-am-getting-exception-in-main-thread-even-when-i-am-handling-the-exception請註冊您的帳戶,並堅持使用同一個帳戶:http://www.google.com/search?q=InvalidStartingStonesException+site%3Astackoverflow.com – BalusC 2010-04-11 04:45:23
標記爲功課? – Anonymoose 2010-04-11 06:10:51