Java: 扫描仪编译错误

Java: scanner compile errors

我目前正在开发一款简单的战舰游戏。我正在使用 Java-scanner 从用户那里获取坐标。如果我在 eclipse 中 运行 这段代码没有错误,一切都很好。但是如果我 运行 在另一个编译器中编译代码(例如 http://www.compilejava.net/),我会得到这个错误:

Exception in thread "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 的工作方式是:

Scanner input = new Scanner(System.in); 

系统是 java.lang.System 并且它有静态对象,即 in 是类型 InputStreamouterr 属于 PrintStream.

类型

基本上 System.in 是 InputStream 对象,它连接到它应该期望数据的来源 - 它们是键盘。

在线 IDE 的设计方式应该是模拟 InputStream 和 Input via Keyboard 等以及 Compile Java that you mentioned where it is not running your code correctly is having issue emulating the input from the Keyboard the way Scanner expect it. The fault is not from your code but, from the online compiler. To proof my point please try the different Java Compiler you find in this link.

如果您想了解 Scanner 在 java 中的工作原理,请参阅我在 Whosebug 中对此 question 给出的答案。在那里我已经详细解释了什么是 Scanner,它是如何工作的。