boolean gotGoodGenInput = false;
while (!gotGoodGenInput)
{
gotGoodGenInput = true;
String inputGen = JOptionPane.showInputDialog
(
"Enter your Generation \n" +
"It must be a number from 0 to 25"
);
if(inputGen != null)
{
if (inputGen.isEmpty() || !inputGen.matches("[0-9]*"))
{
JOptionPane.showMessageDialog
(
null,
"Please input a number from 0 to 25",
"Error",
JOptionPane.ERROR_MESSAGE
);
gotGoodGenInput = false;
}
int GenNumber = Integer.parseInt(inputGen);
if (GenNumber < 0 || GenNumber > 25)
{
JOptionPane.showMessageDialog
(
null,
"Your number can't be less than 0 or greater than 25",
"Error",
JOptionPane.ERROR_MESSAGE
);
gotGoodGenInput = false;
}
}
else
{
System.exit(0);
}
}
你好我遇到的問題是,如果用戶輸入「A」(作爲一個例子),那麼他們將得到錯誤「請輸入一個從0到25」從while循環不設置後重復虛假
if (inputGen.isEmpty() || !inputGen.matches("[0-9]*"))
它應該重新啓動循環,當它命中gotGoodGenInput = false;
但它一直要到下一部分在那裏它會嘗試解析INT但當然會報錯,因爲「A」是不是int
所以我的問題是爲什麼不重新開始時,達到gotGoodGenInput = false;在第一條if語句中。
謝謝修復它 – Mert 2013-02-28 19:54:52