我是編程新手,目前我正在關注一個Java類,我必須驗證用戶的掃描器輸入。下面的代碼似乎工作,但我認爲它有點低效。必須有更好的方法來做到這一點!驗證掃描儀的輸入:是一個整數,並在一定的數字範圍內?
歡迎您的建議!
下面是我得到了什麼:
do {
System.out.println("What is you age?");
while (!scSudoku.hasNextInt()) {
System.out.println("You must enter an number");
scSudoku.next();
}
age = scSudoku.nextInt();
if (age < 10 || age > 90) {
System.out.println("You age must be within 10 and 90");
}
} while (age < 10 || age > 90);
Java的['Scanner'(https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html)不具有'nextln()'方法。 – Andreas
@Andreas它似乎爲我工作。 –
如果是這樣的話,你可以提供鏈接到定義'nextln()'方法的javadoc嗎?也許可以參考「更廣泛使用」的說法或舉例? – Andreas