[英]How to read only one value with Scanner (Java)?
我有多個 Scanner 方法來保存輸入,但是當我執行代碼並輸入例如“word 5”時,Scanner 也會讀取下一個 Scanner 方法的輸入。 我怎么能阻止呢?
...
correctInput = true;
for (int i = 0; i < runnI; i++) {
System.out.println(".....");
array1[i] = sc.next();
System.out.println(".....");
do {
try {
array2[i] = sc.nextDouble();
correctInput= false;
} catch (InputMismatchException e) {
System.out.println("Wrong");
sc.next();
}
} while (correctInput);
}
伙計們,當我在第一個System.out.println()
之前編寫sc.nextLine()
System.out.println()
並在它之后array1[i] = sc.nextLine()
時,我發現它有效
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.