-2
我沒有真正瞭解掃描器的行爲。我想先輸入一個int,然後在while循環中輸入下一行輸入。但是在第一次輸入後,我得到一個ArrayOutOfBoundsException,如下所示。它只是忽略了我想輸入下一行。唯一的解決方案是當我輸入int和新行在一行中用空格分隔時,但這不是我想要的,因爲此時用戶不知道在第一個int後輸入什麼。Java掃描器不期望下一個輸入,忽略nextLine()
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt() - 1;
logic.setGameField(games.get(i).getGameField());
// scanner.reset(); //what does that do?
view.displayField(logic.getCompleteGameField(), logic.getGameSize(), logic.getCompleteGameSize());
double time = System.currentTimeMillis();
while (!logic.gameIsFinished()) {
System.out.println("Spalte Reihe Zeichen: X/*");
String s = scanner.nextLine();
char[] tmp = s.toCharArray();
//just for testing
System.out.println(s.length()); //outputs: 0
System.out.println(tmp.length); //outputs: 0
for (int j = 0; j < tmp.length; j++) {
System.out.print(tmp[j] + " " + j);
}
System.out.println();
logic.setSingleField(tmp[1] - CHAR_TO_INT_OFFSET_ROW, tmp[0] - CHAR_TO_INT_OFFSET_COLUMN, tmp[2]); //throws as expected ArrayIndexOutOfBoundsException
'System.out.println(s.length()); // outputs:0'所以當你使用'tmp [2]','tmp [1]'或'tmp [0]'時,你期望什麼? – 2017-04-23 16:01:06