首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >计算随机数的代码仅生成1

问计算随机数的代码仅生成1
EN

Stack Overflow用户
提问于 2017-05-18 14:17:44
回答 3查看 80关注 0票数 2

代码应该计算一个随机数,你必须猜测它才能得到正确的答案,但当我尝试测试它时,答案总是1,这是为什么呢?以下是代码

代码语言:javascript
复制
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;
}
 }

这是tester类

代码语言:javascript
复制
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的时候,它似乎没有给我任何随机数,答案总是1,请帮帮忙!

EN

回答 3

Stack Overflow用户

发布于 2017-05-18 14:20:56

你正在做这件事

代码语言:javascript
复制
(int) Math.random()

由于Math.random()方法返回一个介于0和1之间的双精度值,因此您会将该双精度值截断为一个整型,结果始终为

尝试修改发生强制转换操作的强制转换,并记住对integer操作的integer返回整数。

代码语言:javascript
复制
target = (int) (maxValue * Math.random()) + 1;
票数 3
EN

Stack Overflow用户

发布于 2017-05-18 14:21:55

Math.random()返回一个介于0和1之间的数字。这意味着(int)Math.random()将 always be 0,这反过来又让this.target = (maxValue * (int)Math.random()) + 1;始终计算为1。为了解决这个问题,只需在计算之外进行强制转换:

代码语言:javascript
复制
this.target = (int)(maxValue * Math.random() + 1);
票数 1
EN

Stack Overflow用户

发布于 2017-05-18 14:24:47

这就是你的问题:

代码语言:javascript
复制
this.target = (maxValue * (int)Math.random()) + 1;

在Math.random()的Javadoc中:

返回一个带正号的双精度值,大于或等于0.0,小于1.0。

因此,您在半开区间[0.0,1.0]中获得一个值,并将其转换为int。由于强制转换为整数只是向下舍入,因此(int)Math.random()的结果将始终为0。将其与maxValue相乘,结果仍为0。加1,结果是this.target总是设置为1。

查看类Random并使用method nextInt(int n)获取区间[0,n[ (请注意,n是独占的))中的随机整数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44039980

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档