2017-05-18 79 views
2

代碼應該計算一個隨機數,你必須猜出它來做出正確的答案,但是當我試圖測試它時,答案總是1,爲什麼是這樣。 這裏是代碼計算隨機數的代碼只產生1個

public class GuessGame 
{ 
private int target; 
private int max; 
private int maxGuesses; 
/** 
    Constructs a new game by randomly selecting a target number 
    between 1 and max_value. Also assigns the maximum number of 
    guesses by calling the calcGuesses method. 
    @param max_value the largest possible target number 
*/ 
public GuessGame(int maxValue) 
{ 
//this.max = maxValue* (int)Math.random() + 1; 
this.target = (maxValue * (int)Math.random()) + 1; 

} 
/** 
Checks if the provided guess is in the range between 1 and max_value. 
@param guess the guess to check 
@return returns true if the guess is in the appropriate range 
*/ 
public boolean isValidGuess(int guess) 
{ 
//implementation not shown 
if(guess >= 1 && guess <= max) 
{ 
    return true; 
    } 
    else 
    { 
     return false; 
    } 
} 
/** 
Checks if the provided guess matches the target number. 
@param guess the guess to check 
@return returns true if the guess matches the target, false otherwise 
*/ 
public boolean isWinner(int guess) 
{ 
    //implementation not shown 
if(guess == target) 
{ 
    return true; 
    } 
    else { 
     return false; 
     } 
    } 
    /** 
Checks if the provided guess is too high or too low 
Precondition: the guess is not equal to the target. 
@param guess the guess to check 
@return returns an appropriate message indicating too high or too low 
    */ 
    public String determineHighLow(int guess) 
    { 
//implementation not shown 
if(guess > target) 
{ 
    return "Too HIGH!"; 
    } 
    else if(guess < target) 
    { 
     return "Too LOW!"; 
    } 
    return "The number you guessed is: " + guess; 

    } 
/** 
Calculates the number of guesses to give the player based on the 
max_value selected. 
    @return returns the number of guesses to give the player 
*/ 
public int calcGuesses() 

{ 
maxGuesses = 10 ; 
return maxGuesses; 
    } 

/** 
Calculates the number of guesses remaining. 
@param guesses the number of guesses the player has used so far 
@return returns the number of guesses remaining 
*/ 
    public int guessesLeft (int guesses) 
{ 
//implementation not shown 
maxGuesses = maxGuesses - guesses; 
return maxGuesses; 
} 
} 

,這裏是測試儀類

import java.util.Scanner; 
public class GuessGameRunner 
{ 
/* 
* The GuessGameRunner class is the main class that works as follows. 
* The main method prompts the user to enter a maximum value(re-prompting if the user enters a 
* number less than 10 or any non-integer), 
* then constructs a new GuessGame object with that valid entered value. 
* The following is then repeated until the player wins or runs out of guesses: 
* player is informed of number of guesses remaining and is prompted to make a guess 
* (re-prompting if invalid); the guess is checked for winning—if not a winner, 
* the player is told whether the guess is too high or low. 
* After the repetition is complete, the appropriate winning or losing message is displayed. 
* The winning message should include how many guesses the player took. 
*/ 

    public static void main(String[] args) 
{ 
int target; 
    Scanner goal = new Scanner (System.in); 
    System.out.println("Guess my number"); 
    target = goal.nextInt() ; 




    GuessGame targetGuess = new GuessGame(10); 
    //targetGuess.GuessGame(target); 
    target = targetGuess.calcGuesses(); 

    System.out.println(targetGuess.calcGuesses()); 
    int bool = goal.nextInt(); 



    while(targetGuess.isWinner(bool)== false && target > 0) 
    { 
    System.out.println(targetGuess.isValidGuess(bool)); 

    System.out.println(targetGuess.isWinner(bool)); 
    if (target != bool) 
    { 
     System.out.println("guesses left: " + targetGuess.guessesLeft(1)); 
     System.out.println(targetGuess.determineHighLow(bool)); 
    } 
    bool = goal.nextInt(); 
} 
System.out.println(targetGuess.isValidGuess(bool)); 
System.out.println(targetGuess.determineHighLow(bool)); 

    System.out.println(targetGuess.isWinner(bool)); 
} 
} 
再次

中的Math.random()時,我乘,加1它似乎並沒有被給我任何隨機數答案總是一個,請幫助!

+0

請寫一個標題來描述你面臨的問題。 – CodingNinja

回答

3

你這樣做

(int) Math.random() 

以來的Math.random()方法返回一個double和0至1之間,你是 截斷該雙成總是導致的INT是

嘗試修改發生演員操作的演員陣列,並記住針對整數操作的整數返回整數。

target = (int) (maxValue * Math.random()) + 1; 
1

Math.random()返回01之間的數字。這意味着(int)Math.random()總是0,這反過來讓this.target = (maxValue * (int)Math.random()) + 1;始終評估爲1。爲了解決這個問題,根本就投的計算之外:

this.target = (int)(maxValue * Math.random() + 1); 
0

這是你的問題:

this.target = (maxValue * (int)Math.random()) + 1; 

從Javadoc文檔的Math.random():

返回具有正號的雙倍值,大於或等於0.0且小於1.0。

所以你在半開區間[0.0,1.0]中得到一個值,並將其轉換爲int。由於鑄造到一個整數只是向下滾動,(int)Math.random()的結果總是將爲0.與maxValue相乘,它仍然是0.另外一個,現在this.target始終設置爲1作爲結果。

檢出類Random並使用method nextInt(int n)獲得區間[0,n [(注意n是唯一的))中的隨機整數。