好的,对于大学,我们必须创建两个Die对象,并将它们滚动几次,计算出现的蛇眼的数量。
这是我拥有的代码,我需要一些帮助,因为每次我编译和执行我的程序时,我都会得到一个失败。我不是百分之百确定我哪里错了,我也不完全知道解决方案是否会有帮助,除非你去我的大学,但任何帮助都是非常感谢的:)谢谢。
失败的是- http://i.imgur.com/ghcOlpP.png
(这都是用JPLIDE编写的)
final int ROLLS = 500;
int num1, num2, count = 0;
Die die1 = new Die();
Die die2 = new Die();
for (int roll=1; roll <= ROLLS; roll++)
{num1 = 1;
num2 = 1;
if (num1 == 1 && num2 == 1) // check for snake eyes
count++;
}
System.out.println ("Number of rolls: " + ROLLS);
System.out.println ("Number of snake eyes: " + count);
System.out.println ("Ratio: " + (float)count / ROLLS);
}}}
class Die
{private final int MAX = 6; // maximum face value
private int faceValue; // current value showing on the die
//-----------------------------------------------------------------
// Constructor: Sets the initial face value of this die.
//-----------------------------------------------------------------
public Die()
{faceValue = 1;
}
//-----------------------------------------------------------------
// Computes a new face value for this die and returns the result.
//-----------------------------------------------------------------
public int roll()
{faceValue = (int)(Math.random() * MAX) + 1;
return faceValue;
}
//-----------------------------------------------------------------
// Face value mutator. The face value is not modified if the
// specified value is not valid.
//-----------------------------------------------------------------
public void setFaceValue (int value)
{if (value > 0 && value <= MAX)
faceValue = value;
}
//-----------------------------------------------------------------
// Face value accessor.
//-----------------------------------------------------------------
public int getFaceValue()
{return faceValue;
}
//-----------------------------------------------------------------
// Returns a string representation of this die.
//-----------------------------------------------------------------
public String toString()
{String result = Integer.toString(faceValue);
return result;
}
}发布于 2015-08-20 09:34:58
for (int roll=1; roll <= ROLLS; roll++) {
num1 = 1;
num2 = 1;
if (num1 == 1 && num2 == 1) // check for snake eyes
count++;
}这看起来不对劲。设置num1 = 1和num2 = 1,然后检查两者是否都等于1。当然,它们将等于1。我假设您的意思是这样
num1 = die1.roll() num2 = die2.roll()
这至少可以让你不会500次都有蛇眼。
https://stackoverflow.com/questions/32107814
复制相似问题