线程“main”中的异常java.lang.IllegalArgumentException:n必须为正数

时间:2010-11-01 19:59:13

标签: java

这是我的错误消息:

Exception in thread "main" java.lang.IllegalArgumentException: n must be positive

   at java.util.Random.nextInt(Random.java:265)

   at Game.generateSecretNumber(Game.java:44)

   at Game.start(Game.java:32)

   at Game.main(Game.java:18

我的代码是:

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

public class Game {
private final int LOWER_BOUND = 1;

private int secretNumber;
private int top;

private static enum Response {YES, NO}
private Random random;

private Scanner scanner;

public static void main(String[] args) {
    Game GuessingGame = new Game();
    GuessingGame.start();
} 
    public Game() {
        random = new Random( );
        scanner = new Scanner(System.in);

    }
     public void start() {

         Response answer;

         answer = Response.YES;

         while (answer == Response.YES) { 
             generateSecretNumber();
             playGame();
             answer = prompt("");

         }

      System.out.println("Bye!"); 

}


     private void generateSecretNumber() {
         secretNumber = random.nextInt(top) + 1;
         System.out.println("You are going to guess a number between 1 and what   number? ");
         top = scanner.nextInt();
         System.out.println("Type a number between 1 and: " + top);
     }

     private void playGame() {
         int guessCount = 0;
         int guess;


         do {

         guess = getNextGuess();
         guessCount++;

           if (guess < secretNumber) {
        System.out.println("Too low.  Try again:");

           }
           else if 
              (guess > secretNumber) {
    System.out.println("Too high.  Try again:");
           }
         }
         while ( guess != secretNumber );
     if ( guess == secretNumber ) {
           System.out.println("Right! It took you " + guessCount + " tries");
     }
     else {
         System.out.println("game over");
     }
     }
     private Response prompt(String question) {

         String input;

         Response response = Response.NO;

         System.out.print(question + "Would you like to play again? ");

         input = scanner.next();

         if (input.equals("Y") || input.equals("y")) {
             response = Response.YES;
         }
         return response;
     }

     private int getNextGuess() {
         int input;

         while(true) {
             System.out.print(" ");
             input = scanner.nextInt();

             if (LOWER_BOUND <= input && input <= top) {
                 return input;
             }
             System.out.println("Invalid input: " + "must be between " + LOWER_BOUND + " and" + top);
         }
     }
   }

3 个答案:

答案 0 :(得分:6)

首次调用top为零,因此random.nextInt(top)会引发异常。只能使用正数调用此方法。

答案 1 :(得分:2)

当你在第44行调用:random.nextInt(top)时,你传递0(因为这是“顶部”变量的值)

你必须通过一些&gt; 0

这就是:“n必须是正面的”意味着。

答案 2 :(得分:0)

您使用

secretNumber = random.nextInt(top) + 1;

在为top分配了一个有意义的值之前(除了默认的0值;随机抛出异常,因为它需要一个正值)。