gpt4 book ai didi

java - 计算随机数的代码仅生成 1

转载 作者:行者123 更新时间:2023-11-30 06:48:36 25 4
gpt4 key购买 nike

代码本应计算一个随机数,您必须猜测它才能得出正确答案,但当我尝试对其进行测试时,答案始终为 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 时,它似乎没有给我任何随机数,答案总是一个,请帮忙!

最佳答案

你在做这个

(int) Math.random()

因为 Math.random() 方法返回一个介于 0 和 1 之间的 double ,你是将该 double 截断为 int 结果始终为 0

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

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

关于java - 计算随机数的代码仅生成 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44039980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com