JAVA:为什么我总是收到这些运行时错误?

JAVA: Why do I keep getting these runtime errors?

写了一个roshambo程序,运行用了一次,运行正常,再用运行,出现了以下错误:

RoShamBo!

  1. 玩游戏
  2. 显示分数
  3. 退出

1

你选哪个?

  1. 摇滚
  2. 论文
  3. 剪刀

2

你选哪个?

  1. 摇滚
  2. 论文
  3. 剪刀

输入无效,请重试。

Exception in thread "main" java.util.NoSuchElementException

at java.util.Scanner.throwFor(Unknown Source)

at java.util.Scanner.next(Unknown Source)

at RoShamBo.getUserChoice(RoShamBo.java:82)

at RoShamBo.winner(RoShamBo.java:112)


at RoShamBo.main(RoShamBo.java:27)

不确定如何处理这些类型的错误,这是我第一次使用方法,所以我认为这与我如何调用每个方法有关?请帮忙

提前致谢。

import java.util.Scanner;

public class RoShamBo 
{
public static void main(String[] args)
{
    System.out.println("RoShamBo!");
    System.out.println("1. Play Game");
    System.out.println("2. Show Score");
    System.out.println("3. Quit");
    Scanner userInput = new Scanner(System.in);

    if(userInput.hasNextInt())
    {
        int userIn = userInput.nextInt();
        if(userIn == 1)
        {
            getUserChoice();
            getCompChoice();
            winner();
        }
        else if(userIn == 2)
        {
            scores();
        }
        else if(userIn == 3)
        {
            System.out.println("Qutiing: Final Score was: ");
            scores();
            userInput.close();
        }
        else
        {
            System.out.println("Invalid Entry, Please Try Again.");
            userInput.next();
        }
    }
    else
    {
        System.out.println("Invalid Entry, Please Try Again.");
        userInput.next();
    }
}      
public static String getUserChoice()
{
    // ask player for a move : playerMove
    System.out.println("Which do you choose?");
    System.out.println("1. Rock");
    System.out.println("2. Paper");
    System.out.println("3. Scissors");

    Scanner input = new Scanner(System.in);
    String userChoice = " ";

    if (input.hasNextInt())
    {
        int userInput = input.nextInt();
        switch(userInput)
        {
            case 1:
                    userChoice = "Rock";
                    break;
            case 2:
                    userChoice = "Paper";
                    break;
            case 3:
                    userChoice = "Scissors";
                    break;
        }

    }
    else
    {
        System.out.println("Invalid Entry, Please Try Again.");
        input.next();
    }
    input.close();
    return userChoice;
}
private static String getCompChoice()
{
    //Method for getting Computers move
    int compChoice = (int) ( 1 + (Math.random() * 3));
    String compMove = " ";
    switch(compChoice)
    {
        case 1:
                compMove = "Rock";
                break;
        case 2:
                compMove = "Paper";
                break;
        case 3:
                compMove = "Scissors";
                break;
    }

    return compMove;
}

public static String winner()
{
    String winnerIs = " ";
    String comp = getCompChoice();
    String user = getUserChoice();
    if(user.equals("Rock") && comp.equals("Scissors") ||
       user.equals("Paper") && comp.equals("Rock") ||
       user.equals("Scissors") && comp.equals("Paper"))
    {          
        System.out.println("You Win!");
    }
    else
    {
        System.out.println("You Lose");
    }
    return winnerIs;
}
public static void scores()
{
    int compCount = 0;
    int userCount = 0;
    if (winner().equals("You Win!"))
    {
        userCount++;
    }
    else
    {
        compCount++;
    }
    System.out.println("Player = " + userCount );
    System.out.println("Computer = " + compCount );
}
}

可能在这里

 {
        System.out.println("Invalid Entry, Please Try Again.");
        input.next();
    }

您正在执行 next() 而没有检查 has*()

你的堆栈给出了一个提示。

at java.util.Scanner.next(Unknown Source).

Exception in thread "main" java.util.NoSuchElementException

转到此处的文档:

http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#next%28%29

您感兴趣的部分:

Throws: NoSuchElementException - if no more tokens are available

你打电话给.next() 但是你想读什么?输入缓冲区中是否有任何等待?另外,请参阅我的评论,这意味着您的逻辑已被破坏。重新思考您的游戏逻辑而不是修复它。

给出一个完整的答案,使您的代码达到预期的 运行 将意味着编写您的大部分代码,我不会那样做。相反,您应该应用人类已知的最强大的调试方法之一并自己动手:

https://en.wikipedia.org/wiki/Rubber_duck_debugging

祝你好运,享受 ;)