简体   繁体   中英

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

Here is my error message:

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

My Code is:

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);
         }
     }
   }

On first invocation top is zero, so random.nextInt(top) throws an exception. This method must only be called with positive numbers.

When you invoke: random.nextInt(top) at line: 44, youre passing 0 ( for that's the value of the "top" variable )

You have to pass something > 0

That's what the: "n must be positive" means.

You use

secretNumber = random.nextInt(top) + 1;

before top has been assigned a meaningful value (apart from the default 0 value ; random throws an exception since it needs a positive value).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM