Java程序提示

时间:2013-05-01 18:21:28

标签: java exception methods

我正在尝试制作一个问答游戏。我想知道当我运行该程序时,我的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

1 个答案:

答案 0 :(得分:1)

这是因为input2null试试这个:

Scanner input2 = new Scanner(System.in);
相关问题