扫描仪元素例外

时间:2015-04-24 02:21:00

标签: java java.util.scanner

我正在做一个简单的Rock Paper Scissors程序,允许用户继续玩。当涉及重复串扫描器的声明时,我一直得到一个元素异常 -

import java.util.Scanner;
import java.util.Random;

public class RockPaperScissors {
    String ai = null;
    String player = null;
    static int playerscore=0, aiscore=0, ties=0, rounds=0;
    static String repeatstring = null;

    public static void main(String[] args) {
        boolean repeat = false;
        RockPaperScissors rps = new RockPaperScissors();
        System.out.println("Welcome to Rock Paper Scissors!\n");
        do {
            System.out.println("Choose your output! R - Rock, P - Paper, S - Scissors");
            rps.user();
            rps.comp();
            rps.game();
            System.out.println("YOU - " + playerscore + " / COMPUTER - " + aiscore);
            System.out.println("Rounds played - " + rounds + "\n");
            System.out.println("Do you wish to continue? Y/N");

            // Error reading string input... Solution pending.
            Scanner repeatinput = new Scanner(System.in);
            repeatstring = repeatinput.nextLine();
            if(repeatstring.equals("Y")) {
                repeat = true;
            }
            else if(repeatstring.equals("N")) {
                repeat = false;
            }
            repeatinput.close();

        } while (repeat == true); 
        System.exit(0);
    }

    void user() {
        Scanner scan = new Scanner(System.in);
        String input = scan.next();
        if(input.equals("R")) {
            player = "Rock";
        }
        else if (input.equals("P")) {
            player = "Paper";
        }
        else if (input.equals("S")) {
            player = "Scissors";
        }
        scan.close();
    }

    void comp() {
        Random rand = new Random();
        int randno = rand.nextInt((3 - 1) + 1) + 1;
        switch (randno) {
            case 1: ai = "Rock";
            case 2: ai = "Paper";
            case 3: ai = "Scissors";
        }
    }

    void game() {
        // If both player and AI has the same output -
        if(ai.equals(player)) {
            ties = ties + 1;
            System.out.println("You chose - " + player);
            System.out.println("Computer chose - " + ai);
            System.out.println("We have a TIE!");
        }

        // ROCK VS SCISSORS
        if(((ai.equals("Rock")) && (player.equals("Scissors"))) || (player.equals("Rock") && (ai.equals("Scissors")))) {
            if(ai.equals("Rock")) {
                aiscore = aiscore + 1;
                System.out.println("You chose - " + player);
                System.out.println("Computer chose - " + ai);
                System.out.println("The COMPUTER WINS!");
            }
            else {
                playerscore = playerscore + 1;
                System.out.println("You chose - " + player);
                System.out.println("Computer chose - " + ai);
                System.out.println("YOU WIN!");
            }
        }

        // ROCK VS PAPER
        if(((ai.equals("Rock")) && (player.equals("Paper"))) || (player.equals("Rock") && (ai.equals("Paper")))) {
            if(ai.equals("Paper")) {
                aiscore = aiscore + 1;
                System.out.println("You chose - " + player);
                System.out.println("Computer chose - " + ai);
                System.out.println("The COMPUTER WINS!");
            }
            else {
                playerscore = playerscore + 1;
                System.out.println("You chose - " + player);
                System.out.println("Computer chose - " + ai);
                System.out.println("YOU WIN!");
            }
        }

        // PAPER VS SCISSORS
        if(((ai.equals("Scissors")) && (player.equals("Paper"))) || (player.equals("Scissors") && (ai.equals("Paper")))) {
            if(ai.equals("Scissors")) {
                aiscore = aiscore + 1;
                System.out.println("You chose - " + player);
                System.out.println("Computer chose - " + ai);
                System.out.println("The COMPUTER WINS!");
            }
            else {
                playerscore = playerscore + 1;
                System.out.println("You chose - " + player);
                System.out.println("Computer chose - " + ai);
                System.out.println("YOU WIN!");
            }
        }
        rounds = rounds + 1;
    }
}

程序在请求重复输入(Y / N)后终止自身,并显示以下错误消息:

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

1 个答案:

答案 0 :(得分:1)

通常,当您获得例外情况时,最好在线查找该例外情况(除非您知道是什么抛出它)。在这种情况下,如果我们查找您收到的异常,我们将被定向到Javadocs,其中声明:

  

由枚举的nextElement方法抛出,表示枚举中没有更多元素。

这导致我们查看扫描程序的Javadoc以查看nextLine()是否抛出了这个问题。我们看到在引发部分下面说:

  

NoSuchElementException - 如果没有找到行

这意味着您的程序正试图从尚未获得结果的扫描仪中获取。如果您希望它等到有结果,那么还有其他可用的方法可以帮助您的程序知道结果何时可用,特别是hasNextLine()。这意味着您应该在尝试获得响应之前检查用户是否已做出响应。

与往常一样,尽可能检查Javadoc是最好的。

另外,最好不要在每次循环运行时都不生成扫描程序,而是在循环之外将其声明,然后在完成循环后关闭它。