所以这就是我想做的。程序应该有一个循环,迭代10次。每次循环迭代时,它都应该同时滚动两个骰子。价值最高的模具获胜。在打成平手的情况下,骰子中的某一卷是没有赢家的。
当循环迭代时,程序应该:
模具目标代码:
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个滚动和确定用户是否赢,计算机赢,或这是一个平分。
发布于 2018-09-14 18:29:44
一种简单而又优雅的解决方案,它使用一个简单的while循环和一对变量来保存用户评分、comp分数以及每个变量的总赢数。
int userWin = 0, compWin = 0;
int MAX_ATTEMPTS = 10;
while(MAX_ATTEMPTS > 0) {
int userScore = 0, compScore = 0;
//Roll the dice for user
die1.roll();
die2.roll();
userScore = die1.getValue() + die2.getValue();
//Roll the dice for comp
die1.roll();
die2.roll();
compScore = die1.getValue() + die2.getValue();
// determine winner
if (userScore > compScore) {
System.out.println("User wins! \nUser score = " + userScore + ", Comp score = " + compScore);
userWin++;
}
else if (userScore < compScore) {
System.out.println("Comp wins! \nUser score = " + userScore + ", Comp score = " + compScore);
compWin++;
} else {
System.out.println("Draw!\nUser score = " + userScore + ", Comp score = " + compScore);
}
MAX_ATTEMPTS --;
}
System.out.println("User won = " + userWin + " times! ");
System.out.println("Comp won = " + compWin + " times! ");发布于 2018-09-14 18:09:03
一个示例解决方案是初始化两个数组,一个用于计算机,一个用于用户。每次抛出骰子时,都会在位置上增加数组,用骰子抛出转弯号。
int [] computer = new int[10];
int [] user = new int [10];
for (int i=0;i<10; ++i) {
int diceUser = throwDice();
int diceComputer = throwDice();
if (diceUser> diceComputer) {
user[i] = diceUser;
}
else if (diceUSer<diceComputer) {
computer[i]= diceComputer;
}
else {
computer[i] = diceComputer;
user[i] = diceUser;
}
}每次计算机或用户丢失时,数组中都会有0。当它是抽签时,两个数组将在相同的索引中包含相同的值。
数组的索引是在转弯之后。
https://stackoverflow.com/questions/52337139
复制相似问题