java - 出现 NoSuchElementException 并且我找不到解决方法

标签 java compiler-errors

我已经为猜词游戏编写了一些代码。它从用户输入中读取字符并在单词中搜索该字符;根据字符是否在单词中,程序返回并控制一些变量。

代码如下:

import java.util.Random;
import java.util.Scanner;
import java.lang.Character;

public class Game {

    private String word;
    private int wordNum;
    private char[] wordinArr = new char[30];
    private char[] wordinDashes = new char[30];
    private int lettersFound = 0;
    int numOfGuesses=10;

    public Game() {

        Random random = new Random();   //επιλογή τυχαίας λέξης
        wordNum = random.nextInt(120);
        //System.out.println("Random Number: "+wordNum);
        word = Lexicon.getWord(wordNum);
        //System.out.println(word); 
    } 

    public void startTheGame() {

        do {


            wordinArr = word.toCharArray();
            System.out.println(wordinArr);
            System.out.println(word.length());
            for(int i=0;i<word.length();i++) 
                wordinDashes[i]='-';
            System.out.println("The random word is now: ");
            System.out.println(wordinDashes);
            System.out.println("You have  "+numOfGuesses+" guesses left.");

            if(MakeATry())
                numOfGuesses-=1;
        }while(numOfGuesses !=0 && lettersFound!=word.length());

        if(lettersFound==word.length())
            System.out.println("Congratulations! You guessed the word: "+word);
        else {
            System.out.println("Unfortunately you didn't find the word. The word was"+word);
            System.out.println("Try again to find the next word!");
        }
    }


    public boolean MakeATry() {

        Scanner input = new Scanner(System.in);
        System.out.println("Your guess: ");
        char guess = input.next().charAt(0);

        if(Character.isLowerCase(guess)){            
            char temp = Character.toUpperCase(guess);
            guess = temp;
        }

        for(int i=0;i<word.length();i++) { 
            if(guess==wordinArr[i]) {
                System.out.println("The guess is CORRECT!");
                wordinDashes[i]=guess;
                input.close();
                lettersFound++;
                return true;
            }
        }           
        input.close();
        return false;       
    }

}

但是当我运行它时,我总是收到此错误:

Exception in thread "main" java.util.NoSuchElementException  
    at java.util.Scanner.throwFor(Unknown Source)  
    at java.util.Scanner.next(Unknown Source)  
    at Game.MakeATry(Game.java:54)  
    at Game.startTheGame(Game.java:37)  
    at Main.main(Main.java:17) 

MakeATry() 方法中的第 54 行是 ->char Guess = input.next().charAt(0); 行。我花了几个小时试图弄清楚如何解决它,但没有结果。有人知道这个错误吗?

提前谢谢您。

最佳答案

而不是

input.close(); 

使用

input.reset();

关于java - 出现 NoSuchElementException 并且我找不到解决方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46913330/

相关文章:

java - 使用 Jython 和 Swing 处理事件

java - Spring Data JPA Distinct - 从单列返回结果

ios - 模拟器和真实环境中的 Xcode 构建错误 "Command/usr/bin/codesign failed with exit code 1"

Swift for in 带括号的循环

java - 为什么在 Java 中使用 BufferStrategy 和 Swing Timer 时会出现空指针异常?

java - 如何重构这段代码?

java - 使用java在目录中新建文件

c - 链接ARM C和Rust

c - 给定的 C 代码是否同时存在语法错误和语义错误?

compiler-errors - SBCL 错误消息 : Any way to improve?