我的代码有问题。
我的任务是让我的代码滚动这两个骰子,我做了1000次,每次两个骰子的总和是7然后我计算增加一个计数。直到循环停在1000.这是我的代码。我试图从班级getTotalFaceValue()
访问PairOfDice
的方法。
//********************************************************************
// Die.java Author: Lewis/Loftus
//
// Solution to Programming Project 5.11
//
// Represents one die (singular of dice) with faces showing values
// between 1 and the number of faces on the die.
//********************************************************************
public 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.
//-----------------------------------------------------------------
public Die()
{
faceValue = 1;
}
//-----------------------------------------------------------------
// Rolls the die and returns the result.
//-----------------------------------------------------------------
public int roll()
{
faceValue = (int) (Math.random() * MAX) + 1;
return faceValue;
}
//-----------------------------------------------------------------
// Face value mutator.
//-----------------------------------------------------------------
public void setFaceValue(int value)
{
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;
}
}
//********************************************************************
// PairOfDice.java Author: Lewis/Loftus
//
// Solution to Programming Project 4.9 and 5.11
//********************************************************************
public class PairOfDice
{
public Die die1, die2;
//-----------------------------------------------------------------
// Creates two six-sided Die objects, both with an initial
// face value of one.
//-----------------------------------------------------------------
public PairOfDice()
{
die1 = new Die();
die2 = new Die();
}
//-----------------------------------------------------------------
// Rolls both dice and returns the combined result.
//-----------------------------------------------------------------
public int roll()
{
return die1.roll() + die2.roll();
}
//-----------------------------------------------------------------
// Returns the current combined dice total.
//-----------------------------------------------------------------
public int getTotalFaceValue()
{
return die1.getFaceValue() + die2.getFaceValue();
}
//-----------------------------------------------------------------
// Returns the current value of the first die.
//-----------------------------------------------------------------
public int getDie1FaceValue()
{
return die1.getFaceValue();
}
//-----------------------------------------------------------------
// Returns the current value of the second die.
//-----------------------------------------------------------------
public int getDie2FaceValue()
{
return die2.getFaceValue();
}
//-----------------------------------------------------------------
// Returns the string representation of this pair of dice.
//-----------------------------------------------------------------
public String toString()
{
return "Die 1: " + die1.getFaceValue() + " Die 2: " +
die2.getFaceValue();
}
}
//
//
//
//
//*******************************************
public class ChidoriDiceRoller
{
public static void main(String [] args)
{
PairOfDice.getTotalFaceValue();
for(int i = 1;i<=1000 ; i++)
{
PairOfDice.getTotalFaceValue();//System.out.print(i + " \n");
}
}
}