2010-11-01 124 views
3

這是我的錯誤信息:異常在線程 「主」 java.lang.IllegalArgumentException異常:n必須是正

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

您的帖子中沒有任何問題。 – 2010-11-01 20:00:52

回答

5

在第一次調用top爲零,所以random.nextInt(top)會引發異常。這個方法只能用正數來調用。

+0

private int top = 1; 我將初始值賦值爲1.現在看起來工作正常。 – chief 2010-11-01 20:12:03

0

您使用

​​

top

已經被分配了一個有意義的值(從默認值0分開;隨機拋出一個異常,因爲它需要一個正值)。

+0

這是不正確的。未定義時,整數爲'0'。 – thejh 2010-11-01 20:06:35

+0

@thejh:'0'是未初始化時的默認值,是(編輯一點以進一步闡明)。 – ChristopheD 2010-11-01 20:07:46

+0

即使沒有澄清,這是不正確的。也許不完整... – MForster 2010-11-01 21:41:29

2

當你調用:random.nextInt(top)在行:44,你就是傳遞0(對於這就是「頂」變量的值)

你要通過什麼> 0

這是什麼:「 n必須是正數「的意思。

相關問題