【问题标题】:Java program tips [duplicate]Java程序提示[重复]
【发布时间】:2013-05-01 18:21:27
【问题描述】:

我正在尝试制作一个问答游戏。我想知道当我运行程序时,我的while 语句出现了无限循环。当我进入游戏后运行我的代码时,它会打印问题,但在我输入答案之前,它会给我一个Exception 错误。我不太确定如何解决它。

/**
     * @(#)QuizGameFinal2.java
     *
     *
     * @author
     * @version 1.00 2013/4/30
     */

    import java.util.Scanner;
    import java.lang.Math;
    import java.lang.String;
    public class QuizGameFinal2
    {

        public static void main(String[]args)
        {

    int option_Selected = 0;
        int option_Single_Player = 1;
        int option_Multiplayer = 2;
        int answer = 0;
        int player_One_Answer = 0;
        int player_Two_Answer = 0;
        String response;
        int player_One_Winnings = 0;
        int player_Two_Winnings = 0;
        int winnings = 0;
        int computer_Winnings = 0;
        double computer_Answer = 0;

        Scanner input2 = new Scanner(System.in);



        say_Intro();







        say_Before_First_Question();
        question_One();
        human_Answer();
        while (answer < 1 && answer >= 4);
        {
            System.out.println("Enter a number between 1 and 3");
        }

        if (answer == 1)
        {
            System.out.println("Correct Next Question");
            question_Two();
            human_Answer();
                while (answer < 1 && answer >= 4);
                {
                System.out.println("Enter a number between 1 and 3");
                }
            if (answer == 1 )
            {
                System.out.println("Correct Next Question");
                question_Three();
                human_Answer();

                 while (answer < 1 && answer > 4);
                 {
                    System.out.println("Enter a number between 1 and 3");
                 }

                if (answer == 4)
                {
                    System.out.println("Correct Next Question");

                }

                    else
                    {
                        System.out.println("Incorrect you win 1000 dollers");
                        winnings = 1000;
                    }

            }

                    else
                    {
                        System.out.println("Incorrect you win 500 dollers");
                        winnings = 500;
                    }


        }

        else
        {
            System.out.println("Incorrect you win 0 dollers");
            winnings = 0;
        }





        } // End of main method




        public static void say_Intro()      // Intro Method
        {
        System.out.println("Welcome to the QuizGame");          // Player selects which mode
        }


        public static void say_Before_First_Question()      // Before game method
        {


                System.out.println("");
                System.out.println(" If you get a question wrong your out.");
                System.out.println("Also you will be competing against a super computer, after you play then he will generate answers, if you have the most then you win");
                System.out.println("Ok first question");
        }

        public static void human_Answer ()          // Human Answer Single Player Method
        {

             Scanner input2 = null;
            Object answer = input2.nextInt();
        }

        static void player_One_Answer()     // Player One Multiplayer Method
        {
             Scanner input2 = null;
            int player_One_Answer = input2.nextInt();
        }

        public static void player_Two_Answer()      // Player two multiplayer Method
        {
             Scanner input2 = null;
            int player_Two_Answer = input2.nextInt();
        }

        public static void computer_Answer ()       // Computer answer
        {
             double computer_Answer = (1-1 + 1) * Math.random() + 1;
             computer_Answer = (int)computer_Answer;
        }

        public static void question_One()           // Question 1 method
        {
            System.out.println("");
            System.out.println("What is an application");
            System.out.println("1: A program that performs a task   2:A mouse   3: java.util.Scanner");


        }

        public static void question_Two()       // Question 2 to 10 methods below
        {

            System.out.println("What is the data type that hold the value 1");
            System.out.println("1: int  2:float 3: short 4: long ");

        }

        public static void question_Three()
        {
            System.out.println("What is a not a high level language");
            System.out.println("1: Java     2:C++   3: Colbolt      4: Machine Language ");
        }

    /*  public static void question_Five()
        {

        }

        public static void question_Six()
        {

        }

        public static void question_Seven()
        {

        }

        public static void question_Eight()
        {

        }

        public static void question_Nine()
        {

        }

        public static void question_Ten()
        {

        }

        public static void total_Winnings_Single_Player ()      // Calculating who wins method single player
        {
            if (computer_Winnings > winnings)
            {
                System.out.println("Computer Wins");
            }

                else
                {
                    System.out.println("You win");
                }
                */
        }



    // I get infinite loop om my while statements and I get an exepction error on my values with null

【问题讨论】:

  • 尝试trim代码并发布异常堆栈跟踪。
  • 请自动格式化代码(在 Eclipse 中为 Ctrl+Shift+F)
  • 您的帖子需要格式化、修剪和更多信息(堆栈跟踪在哪里?)
  • 您在尝试在您的回答方法中使用它之前将您的扫描仪无效,这显然会导致 NPE。如果答案是 3,您的第一个 while 循环也将永远运行。除此之外,您的代码还有很多其他问题..

标签: java exception methods


【解决方案1】:

这是因为input2null 试试这个:

Scanner input2 = new Scanner(System.in);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-20
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多