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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4072594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:16:44  来源:igfitidea点击:

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

java

提问by chief

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

采纳答案by MForster

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

第一次调用时top为零,因此random.nextInt(top)抛出异常。此方法只能使用正数调用。

回答by ChristopheD

You use

你用

secretNumber = random.nextInt(top) + 1;

secretNumber = random.nextInt(top) + 1;

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

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

回答by OscarRyz

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

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

You have to pass something > 0

你必须通过一些> 0

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

这就是:“n 必须为正”的意思。