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

时间:2015-10-12 07:38:35

标签: java debugging methods runtime-error

写了一个roshambo程序,运行一次,工作正常,再次运行并弹出以下错误:

RoShamBo!

  1. 玩游戏
  2. 显示分数
  3. 退出
  4. 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 );
        }
        }
        

2 个答案:

答案 0 :(得分:2)

可能在这里

$scope.save = function () {
            $.ajax({
                type: 'POST',
                contentType: 'application/json; charset=utf-8',
                data: JSON.stringify({ "id": "1" }),
                url: "/Home/SaveData",
                success: function (data) {
                    alert(data.status);
                },
                error: function () {
                    alert("error");
                }
            });
        };

    }]);

您正在 { System.out.println("Invalid Entry, Please Try Again."); input.next(); } 而不检查next()

答案 1 :(得分:2)

你的筹码给出了一个提示。

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

您感兴趣的部分:

  

抛出:       NoSuchElementException - 如果没有更多令牌可用

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

给出一个完整的答案,让你的代码运行,就像它应该意味着编写你的大部分代码,我不会那么走。相反,您应该应用人类已知的最强大的调试方法之一并自己完成:

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

祝你好运并享受;)