生成随机数时出现空指针异常

时间:2014-02-19 22:16:31

标签: java

我有一个程序,我需要为一个给我带来麻烦的项目编写。我正在写一个猜谜游戏,它允许你尝试猜测一个数字,并为你提供一个数字,计算机将尝试猜测它。我的主要问题是让计算机猜测,当我试图猜测它给我一个空指针异常,我无法弄清楚如何。我知道这很多但是我会非常感激任何帮助。这是我的程序...(我会把/////放在我得到空指针异常的地方)

import java.util.Scanner;
public class NumberGuessClient {
public static void main(String[]args)    {

  Scanner kb = new Scanner (System.in);
  Die number1 = new Die(99);
  int guess;
  String highLow;

System.out.println("What is your name?");
HumanPlayer player1 = new HumanPlayer(kb.next(), 0, 0, 100, 0, 0);
ComputerPlayer computer = new ComputerPlayer(100, "Computer", 0, 0, 0, 0, 100, 1);
System.out.println(player1.toString());

System.out.println("How many points would you like to wager?");
player1.setWager(kb.nextInt());
while(player1.getWager() > player1.getBalance())
{
  System.out.println("You don't have that many points to wager.");
  System.out.println("How many points would you like to wager?");
  player1.setWager(kb.nextInt());
}

//计算机选择号码,人类玩家猜测

  int num = number1.roll();
  player1.setGuess(-5);
  int guesses = 0;

  while (player1.getGuess() != num)
  {         
     System.out.println("Enter your guess.");
     player1.setGuess(kb.nextInt());
     if(player1.getGuess() < num)
     {
        System.out.println("Guess is too low!");
        guesses++;
     }

     else
     {
        if(player1.getGuess() > num)
           System.out.println("Guess is too high!");
           guesses++;
     }
  }
  System.out.println("Congratulations!!! You took " + guesses + " turns to guess correctly.");
  if(guesses >= 10){
  System.out.println("You took to many guesses. You lose.");
  player1.lose();
  System.out.println(player1.toString());
  }
  else{
  System.out.println("You win!");
  player1.win();
  System.out.println(player1.toString());
  }

  System.out.println("Computer's turn to guess...");

//人类选择号码,计算机猜测

  System.out.println(computer.toString());

  System.out.println(computer.getName() + " wagers " + computer.getWager() + " points");
  System.out.println("What is the number the computer needs to guess?");
  num = kb.nextInt();
/////guess = computer.guess();

   guesses = 0;

  while(guess != num){
     guess = computer.guess();
     System.out.println(computer.getName() + " guessed " + guess + ".");
     System.out.println("Is this higher, lower, or at your number? higher/lower/at");
     highLow = kb.next();
     if(highLow.equals("higher"))
     {
        if(guess < computer.getHigh()){
           computer.setHigh(guess);
        }
     }

     if(highLow.equals("lower"))
     {
        if(guess > computer.getLow()){
           computer.setLow(guess);
        }
     }
     guesses++;
  }  

  System.out.println("The computer guessed your number in " + guesses + " tries.");
  if(guesses <= 10){
     System.out.println("It won!");
     computer.win();
  }

  if(guesses > 10){
     System.out.println("It took too many guesses. It lost.");
     computer.lose();
  }
  computer.toString(); 
  }
  }

这是计算机的方法......

/* This is the class for a computer player for the guessing game
 * A Computer Player has a name, balance, wager, and tracks its wins and losses.
*/
import java.util.Random;

public class ComputerPlayer {

private static Random rand; 
private int balance;
private String name;
private int wager;
private int wins;
private int losses;
private int guess = 1;
private int high;
private int low;

public ComputerPlayer(int bl, String nm, int wr, int ws, int ls, int gs, int hg, int lw) {
  balance = bl;
  name = nm;
  wr = wager;
  ws = wins; 
  gs = guess;
  ls = losses;
  hg = high;
  lw = low;
}


//////public int guess(){   
//////return rand.nextInt(high - low) + low;
}

public void win() {
   wins ++;
}

public void lose() {
   losses ++;
}

public double getBalance() {
   return balance;
}

public int getWager() {
   return 50;
}

public String getName() {
   return name;
}

public int getLosses() {
   return losses;
}

public int getWins() {
   return wins;
}

public void setHigh(int x)
{
   high = x;
}

public void setLow(int x)
{
   low = x;
}

public int getHigh()
{
   return high;
}

public int getLow()
{
   return low;
}




@Override
public String toString() {
   return "Player{" + "name=" + name + ", balance=" + balance + ", wins=" + wins + ", losses="  +  losses + '}';
 }

public void setBalance(int bal) {
   balance = balance;
}
}

1 个答案:

答案 0 :(得分:4)

在ComputerPlayer.java中:

private static Random rand;

您永远不会设置此字段。如果不将它们设置为对象实例,则Java字段为null; null是一个值,可以应用于引用Object的任何字段,包括Random。当您尝试引用null的方法或字段时,Java会抛出NullPointerException。尝试:

private static Random rand = new Random();

如果您想要更准确地定义null,请尝试this StackOverflow answer。 (警告:确实有点技术性。)