【问题标题】:Java - Why is this code repeating? NoSuchElementException?Java - 为什么这段代码重复?没有此类元素异常?
【发布时间】:2018-06-13 01:35:17
【问题描述】:

我正在通过尝试在 Eclipse 中制作一个简单的石头剪刀布游戏来学习 Java:

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, or Scissors");重复自己?谢谢。

【问题讨论】:

标签: java


【解决方案1】:

首先,不要关闭扫描仪。这会导致 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?

【讨论】:

    猜你喜欢
    • 2016-01-16
    • 2014-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多