掃描器我想讀我能夠讀取前兩個數字,當我嘗試閱讀我得到一個異常的符號這種格式爲什麼不工作
4 4
* . . .
. . . .
. * . .
. . . .
4 4
* * . .
. . . .
. . . .
. . * .
的輸入。我可以使用BufferedReader來讀取每一行並解析輸入,但爲什麼我無法使用掃描器來執行此操作?
這是我的代碼
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
Scanner s = new Scanner(in.nextLine());
if (!s.hasNextLine())
break;
int x = s.nextInt(); // 4
int y = s.nextInt(); // 4
for (int i = 0; i <= x - 1; i++) {
for (int j = 0; j <= y - 1; j++) {
System.out.println(s.hasNext()); // false
String symbol = s.next(); //NoSuchElementException WHY?
}
}
}
}
}
看看更新後的Input.But我試圖讓符號在for循環中使用Next代碼在 –
處打破感謝看來我使用了兩個掃描器,以便我可以退出循環,搞砸了。我將只使用一個掃描器 –