我有一個有點麻煩,我的代碼:整數掃描儀驗證作用不是應該
int item = -1;
Scanner input = new Scanner(System.in);
do {
while (!input.hasNextInt() || input.nextInt() > 5) {
System.out.println("Enter a valid integer!");
getMenu();
input.next();
}
item = input.nextInt();
} while (item <= 0);
input.close();
如果我輸入任何低於5它的工作原理。如果我輸入6,它表明我必須輸入一個有效的整數。當我嘗試再次輸入6時什麼都沒有發生,但是當我再次輸入6時,它會再次向我顯示該消息。我認爲這與input.next()
有關,但不太清楚爲什麼。
任何想法?
getMenu()
只是顯示一串字符串順便說一句。
換句話說。用戶必須輸入一個介於1和5之間的有效整數。如果用戶不這樣做,他將循環訪問要求輸入有效整數的代碼塊。用戶將循環直到他輸入一個有效的整數。
什麼是'input.next();'應該這樣做?你沒有把它分配給任何東西。 –
我無法得到你想要達到的目標,請分享它,以便我們可以用更好的代碼來幫助你 –
另外,如果'input.nextInt()> 5'結果是錯誤的,那麼你不會存儲或者,'item'最終得到* next *被掃描的整數。 –