这就是我想要做的。 该程序应具有循环10次的循环。每次循环迭代时,都应掷两个骰子。价值最高的骰子获胜。如果是平局,那么掷骰子没有赢家。
在循环迭代时,程序应:
Die对象代码:
import java.util.Random;
/**
The Die class simulates a six-sided die.
*/
public class Die
{
private int sides; // Number of sides
private int value; // The die's value
/**
The constructor performs an initial
roll of the die.
@param numSides The number of sides for this die.
*/
public Die(int numSides)
{
sides = numSides;
roll();
}
/**
The roll method simulates the rolling of
the die.
*/
public void roll()
{
// Create a Random object.
Random rand = new Random();
// Get a random value for the die.
value = rand.nextInt(sides) + 1;
}
/**
getSides method
@return The number of sides for this die.
*/
public int getSides()
{
return sides;
}
/**
getValue method
@return The value of the die.
*/
public int getValue()
{
return value;
}
}
这是使用目标代码进行骰子及其移动的代码。
public class MitchellLab06
{
public static void main(String[] args)
{
final int DIE1_SIDES = 6; //Number of sides for die #1
final int DIE2_SIDES = 6; //Number of sides for die #1
final int MAX_ROLLS = 10; //Number of ties to roll
// Create two instances of the Die class.
Die die1 = new Die(DIE1_SIDES);
Die die2 = new Die(DIE2_SIDES);
//Display the initial value of the dice.
System.out.println("This program simulates the rolling of a " +
DIE1_SIDES + " sided die and another " +
DIE2_SIDES + " sided die.");
System.out.println("The initial value of the dice:");
System.out.println(die1.getValue() + " " + die2.getValue());
//Roll the dice 10 times.
System.out.println("Rolling the dice " + MAX_ROLLS + " times");
for(int i = 0; i < MAX_ROLLS; i++)
{
//Roll the dice.
die1.roll();
die2.roll();
//Display the value of the dice.
System.out.println(die1.getValue() + " " + die2.getValue());
}
}
}
我需要帮助来跟踪10次掷骰中哪个骰子获胜,并确定用户是否获胜,计算机获胜还是平局。