这个项目正在滚动一个 6 面骰子,并希望它在 6 90% 的时间和其他 1-5 2% 的时间降落。
这是主类 Die
public class Die
{
//----------------------------------------------------------------------------------
// Instance Variables
//----------------------------------------------------------------------------------
protected int numSides;
protected Random myRandomNumGenerator;
//----------------------------------------------------------------------------------
//Constructors
//----------------------------------------------------------------------------------
/**
* Default constructor creates a 6-sided die.
*/
public Die()
{
numSides = 6;
myRandomNumGenerator = new Random();
}
/**
* This constructor takes in a single integer value and create a die with
* that number of sides.
*
* @param aNumSides Number of sides to create on the die.
*/
public Die(int aNumSides){
numSides = aNumSides;
myRandomNumGenerator = new Random();
}
//----------------------------------------------------------------------------------
//Class Methods
//----------------------------------------------------------------------------------
/**
* Getter for the number of sides on the die.
*
* @return The number of sides on the die.
*/
public int getNumOfSides()
{
return numSides;
}
/**
* Rolls the die to get a random value.
*
* @return A random value between 1 and the number of sides.
*/
public int roll()
{
return myRandomNumGenerator.nextInt(numSides) + 1;
}
}
这是我需要覆盖 die 中的 roll 方法以将 die 加载到正确值的子类。
import java.util.Random;
public class LoadedDie extends Die{
protected double loadedPercentage;
protected int numSides;
protected int loadedValue;
protected Random ranNumGenerator;
public LoadedDie() {
loadedValue = 6;
loadedPercentage = .9;
}
public LoadedDie(int aNumSides, int aLoadedValue, double aLoadedPercentage) {
numSides = aNumSides;
loadedValue = aLoadedValue;
loadedPercentage = aLoadedPercentage;
}
public int roll() {
return ranNumGenerator.nextInt(numSides)+1;
}
}
我还没有弄清楚如何使随机数比其他人更频繁地出现。谢谢