2016-05-06 12 views
-1

明日の予定をしようとしています。私はコンパイルするコードを手に入れましたが、実行されません。私はコードを含んでいます。確立された方法はそのままでなければならない。私は非常に助けることができる誰にも感謝しています。スレッド "main"の例外NimGame.main(NimGame.java:27)のjava.lang.NullPointerException

import java.util.Scanner; 
import java.util.Random; 


public class NimGame 
{ 

    public static int remaining; //the number of objects remaining in the game. 
    public static Scanner keyboardInput; //for getting user input from the terminal. 

    /** 
     When run, your main method should allow a user to play a simple version of Nim from the terminal. 
     You should at least implement the methods provided here as described in the assignment guidelines. 
    **/ 
    public static void main(String[] args){ 
    System.out.println("Please enter a starting number."); 
    remaining = keyboardInput.nextInt(); 
    while(remaining>0){ 
     int cMove=getComputerMove(remaining); 
     System.out.println("The computer takes away"+cMove); 
     remaining-=cMove; 
     System.out.println("Now there are"+remaining+" left"); 
     if(remaining<=0){ 
      System.out.println("The computer takes this game."); 
      return; 
     } 
     System.out.println("Please pick a number 1 or 2"); 
     int hMove=getHumanMove(remaining); 
     remaining-=hMove; 
     System.out.println("Now there are"+remaining+" left"); 
     if(remaining<=0){ 
      System.out.println("Congratulations you win."); 
      return; 
     } 
     } 
    } 




    /** 
     Returns a number of objects for the computer to remove on its turn. 
     At a minimum, this should remove the last object if only one remains, and 
     otherwise it should randomly pick between removing one or two objects. 
     (update this documentation with details about the version you actually implement.) 
    */ 
    public static int getComputerMove(int remaining) 
    { 

     Random comp = new Random(); 
      int computerMove = comp.nextInt((2-1)+1)+1; 
     return computerMove; 
    } 

    /** 
     Returns a number of objects for the player to remove on its turn. 
     Use the Scanner parameter to get user input, but verify that the user can only select one or two objects to be removed. 
     (update this documentation with details about the version you actually implement.) 
    */ 
    public static int getHumanMove(int remaining) 
    { 
     System.out.println("Please enter a one or two."); 
     int HumanMove = keyboardInput.nextInt(); 

     return HumanMove; 
    } 

    /** 
     Returns whether or not the game is over, and print the winner to the terminal. 
    */ 
    public static boolean isWin(int remaining) 
    { 
     return false; 
    } 





} 

コードは、「開始番号を入力してください」というメッセージが表示されますが、その後のエラーコードException in thread "main" java.lang.NullPointerException at NimGame.main(NimGame.java:27)が表示されます。

+2

あなたは決してあなたの 'スキャナ'を初期化しませんでした。 –

答えて

0

使用

public static void main(String[] args){ 
    Scanner keyboard = new Scanner(System.in); 
    System.out.println("Please enter a starting number."); 
    remaining = keyboardInput.nextInt(); 
    while(remaining>0){ 
0

強行は、あなたのコード内でpublic static Scanner keyboardInput = new Scanner(System.in);public static Scanner keyboardInput;を変更します。あなたはすべて設定されています

関連する問題