我在試圖弄清楚如何防止用戶輸入數字時遇到了一些麻煩。我瞭解如何防止非數字輸入(即輸入一個字母而不是數字),而不是相反。我如何解決這個問題?嘗試捕獲數字輸入
String[] player_Name = new String[game];
for (i = 0; i < game; i++) {
try {
player_Name[i] = JOptionPane.showInputDialog("Enter the name of the
player, one by one. ");
} catch(Exception e) {
JOptionPane.showMessageDialog(null, "Enter a valid name!");
i--;
}
可能[如何使用掃描器只接受有效的int作爲輸入]的副本(http://stackoverflow.com/questions/2912817/how-to-use-scanner-to-accept-only-valid-int-as-input) – tnw
不要使用異常來控制程序流!例外是錯誤處理。 –
@tnw他想要的是相反的 – litelite