2013-05-01 83 views
-5

我想做一個問答遊戲。我想知道當我運行這個程序時,我在while聲明中遇到了一個無限循環。當我進入遊戲後運行我的代碼時,它打印出問題,但在輸入我的答案之前,它給我一個Exception錯誤。我不太清楚如何解決這個問題。Java程序技巧

/** 
    * @(#)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 
+6

嘗試[裝飾](http://sscce.org)代碼和發佈異常堆棧跟蹤。 – Lion 2013-05-01 18:23:16

+0

請自動填寫代碼(在Eclipse中爲Ctrl + Shift + F) – 2013-05-01 18:24:32

+0

您的文章需要格式化,修剪和更多信息(堆棧跟蹤在哪裏) – jonney 2013-05-01 18:25:56

回答

1

這是因爲input2null試試這個:

Scanner input2 = new Scanner(System.in);