Java:扫描仪行被忽略

时间:2018-07-15 10:29:23

标签: java java.util.scanner

我正在学习Java并且正在制作一个hang子手游戏,但是这一行似乎只是被忽略了而没有被执行。如果我将相同的代码放到更高的位置,那么它将被运行,而它的while循环则不会被运行。 (“字符猜测= reader.nextLine()。charAt(0);”未运行,并且没有输入请求)。 编辑:我已经添加了reader.nextLine(); int输入之后,但仍然无法正常工作。

错误:

Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Unknown Source)
    at test.Testtt.main(Testtt.java:30)

代码:

public class Testtt {

    public static void main(String[] args) {

        Scanner reader = new Scanner(System.in);

        print("Would you like to play singleplayer or multiplayer (s/m)? ");
        String mode = reader.nextLine();

        print("How many game would you like to play?");
        int game_num = reader.nextInt();
        reader.nextLine();

        int score = 0;

        for(int x = 0; x < game_num; x++) {
            String word = get_word(mode);
            String temp = get_temp(word);
            int lives = 6;
            String[] guesses = new String[26];

            while(lives > 0) {
                print("Enter letter");
                char guess = reader.nextLine().charAt(0);
                temp = update_temp(temp, word, guess);

                if(win_check(temp, word)) {
                    score = score + 1;
                    break;

                }
                else {
                    lives = lives - 1;
                    display(lives);
                }
            }
        }
    }

0 个答案:

没有答案