Java - 为什么这段代码会重复? NoSuchElementException异常?

时间:2018-06-13 01:35:17

标签: java

我正在学习Java,试图在Eclipse中制作一个简单的Rock-Paper-Scissors游戏:

package com;


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


public class Game {

    public static void main(String[] args) {
        Game game = new Game();
        game.playerChoice();
        game.aiChoice();
        game.playGame();
    }

public void playGame() {

    if (playerChoice() == "Rock") {
        if (aiChoice() == "Rock") {
                System.out.println("Tie!");
            } else if (aiChoice() == "Paper") {
                System.out.println("AI wins!");
                } else {
                System.out.println("You win!");
                }

        } else if (playerChoice() == "Paper") {
            if (aiChoice() == "Rock") {
                System.out.println("You win!");
            } else if (aiChoice() == "Paper") {
                System.out.println("Tie!");
            } else {
                System.out.println("AI wins!");
            }

        } else {
            if (aiChoice() == "Rock") {
                System.out.println("AI wins!");
            } else if (aiChoice() == "Paper") {
                System.out.println("You win!");
            } else {
                System.out.println("Tie!");
            }
        }
    }

    public String playerChoice() {

        Scanner scanner = new Scanner(System.in);
        String word = null;

        try {
            System.out.println("Type Rock, Paper, or Scissors");
            word = scanner.next();
            System.out.println(word + " is saved as your choice");
        } finally {
            scanner.close();
        }
        return word;
    }

    public String aiChoice() {

        String[] wordlist = {"Rock", "Paper", "Scissors"};
        String word = wordlist [new Random().nextInt(wordlist.length)];
        System.out.println("AI has randomly picked " + word);
        return word;
    }
}

当我在扫描仪输入中输入“Rock”,“Paper”或“Scissors”时,我会在控制台中输入以下内容:

Type Rock, Paper, or Scissors
Rock <--- This is what I input into the console
Rock is saved as your choice
AI has randomly picked Scissors
Type Rock, Paper, or Scissors <--- This unintentionally repeats
Exception in thread "main" java.util.NoSuchElementException
    at java.base/java.util.Scanner.throwFor(Unknown Source)
    at java.base/java.util.Scanner.next(Unknown Source)
    at com.Game.playerChoice(Game.java:53)
    at com.Game.playGame(Game.java:17)
    at com.Game.main(Game.java:12)

我做错了导致此异常的原因以及为什么System.out.println(“Type Rock,Paper或Scissors”);重复一次?谢谢。

1 个答案:

答案 0 :(得分:0)

首先,不要关闭扫描仪..这导致NoSuchElement异常

您可以将扫描仪移动到字段变量,而不是为每个玩家选择创建一个新变量

public class Game {

    private Scanner sc = new Scanner(System.in);

    public static void main(... 

要解决其他问题,在游戏开始前没有任何目的可以选择

玩游戏

 public static void main(String[] args) {
    Game game = new Game();
    game.playGame();
}

然后,你需要坚持每个玩家的选择而不是每次方法调用再次选择(导致你的输入重复,并且AI为每次比较选择新的选择)

public void playGame() {
    final String p = playerChoice();
    final String a = aiChoice();

     if (p.equals(a)) {
         if (a.equals("Rock")) {

请阅读How do I compare strings in Java?