NimGame.main中线程“main”java.lang.NullPointerException中的异常(NimGame.java:27)

时间:2016-05-06 04:14:22

标签: java

我正在尝试明天到期的作业。我已经获得了编译的代码,但它不会运行。我包括了代码。建立的方法必须保持。我非常感谢能够提供帮助的任何人。

 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 :(得分: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){

答案 1 :(得分:0)

Jus在您的代码中将public static Scanner keyboardInput;更改为public static Scanner keyboardInput = new Scanner(System.in);。你已经完成了