[英]Java: scanner compile errors
我目前正在開發一個簡單的戰艦游戲。 我正在使用 Java 掃描儀從用戶那里獲取坐標。 如果我在 eclipse 中運行這段代碼,我不會出錯,一切都很好。 但如果我在另一個編譯器(例如http://www.compilejava.net/ )中運行代碼,我會收到此錯誤:
線程“main”中的異常 java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Scanner.java:1540) at SchiffeVersenken.attack(SchiffeVersenken.java:57) at SchiffeVersenken.main(SchiffeVersenken.java: 38)
所以我知道問題與掃描儀和“.nextLine()”方法有關,但我是 Java 新手,不知道如何解決這個問題。
import java.util.Scanner;
public class SchiffeVersenken {
// "Lebenspunkte" der Spieler
static int userLife = 30;
static int enemyLife = 30;
// Spielbrett beider Spieler
static char[][] user = {
{ '#', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
{ '#', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
{ '.', '#', '#', '#', '#', '#', '.', '.', '.', '.' },
{ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
{ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
{ '.', '.', '.', '.', '.', '.', '#', '#', '#', '.' },
{ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
{ '.', '.', '.', '.', '.', '#', '#', '#', '#', '#' },
{ '.', '.', '.', '.', '.', '#', '#', '#', '#', '#' },
{ '.', '.', '.', '.', '.', '#', '#', '#', '#', '#' }
};
static char[][] enemy = {
{ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
{ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
{ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
{ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
{ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
{ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
{ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
{ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
{ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
{ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' }
};
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while(userLife > 0 && enemyLife > 0) {
attack(s);
defend(s);
}
s.close();
if(userLife == 0) {
System.out.println("Alle Schiffe sind versenkt worden. Sie haben leider verloren!");
} else {
System.out.println("Alle Schiffe des Gegners sind versenkt worden. Sie haben gewonnen!");
}
}
public static void attack(Scanner s) {
String inputHit;
int inputRow, inputColumn;
System.out.println("Bitte Zeile des Ziels eingeben: ");
inputRow = Integer.parseInt(s.nextLine());
System.out.println("Bitte Spalte des Ziels eingeben: ");
inputColumn = Integer.parseInt(s.nextLine());
System.out.println("Koordinaten des Zeils: " + inputRow + "/" + inputColumn);
System.out.println("Ziel getroffen? (y oder n)");
inputHit = s.nextLine();
if(inputHit.equals("y") | inputHit.equals("n")) {
if(inputHit.equals("y")) {
enemy[inputRow][inputColumn] = 'X';
System.out.println("Boom! Guter Schuss!");
enemyLife -= 1;
} else {
enemy[inputRow][inputColumn] = 'O';
System.out.println("Oh je! Leider nicht getroffen!");
}
} else {
System.out.println("Ziel getroffen? (y oder n)");
inputHit = s.next();
}
print(enemy);
}
public static void defend(Scanner s) {
int inputRow, inputColumn;
System.out.println("Bitte Zeile des Ziels eingeben: ");
inputRow = Integer.parseInt(s.nextLine());
System.out.println("Bitte Spalte des Ziels eingeben: ");
inputColumn = Integer.parseInt(s.nextLine());
System.out.println("Koordinaten des Zeils: " + inputRow + "/" + inputColumn);
if(user[inputRow][inputColumn] == '#') {
user[inputRow][inputColumn] = 'X';
System.out.println("Boom! Schiff unter Beschuss!");
userLife -= 1;
} else {
user[inputRow][inputColumn] = 'O';
System.out.println("Gott sei Dank! Nicht getroffen!");
}
print(user);
}
public static void print(char[][] grid) {
System.out.println(" 0123456789");
for(int i=0; i<10; i+=1) {
System.out.print(i);
for (int j=0; j<10; j+=1) {
System.out.print(grid[i][j]);
if(j == 9) {
System.out.println();
}
}
}
System.out.println();
}
}
如果代碼在 IDE 中運行,那么一切都是正確的。 掃描儀的工作方式是:
Scanner input = new Scanner(System.in);
系統是java.lang.System
,它即在被類型的具有靜態對象InputStream
,並ERR是類型PrintStream
。
基本上System.in
是 InputStream 對象,它連接到它應該期望數據的源 - 它們是鍵盤。
在線 IDE 的設計方式應該是它應該通過鍵盤等模擬 InputStream 和 Input,而您提到的Compile Java在沒有正確運行代碼的情況下在模擬來自鍵盤的輸入時遇到問題 Scanner期待它。 錯誤不是來自您的代碼,而是來自在線編譯器。 為了證明我的觀點,請嘗試您在此鏈接中找到的不同 Java 編譯器。
如果您想了解 Scanner 在 Java 中的工作原理,請參閱我在 Stackoverflow 中對這個問題的回答。 我在那里詳細解釋了什么是掃描儀,它是如何工作的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.