我的目標是使用使用Die類的PairOfDice類滾動一對骰子1000次。當滾動1000次,每次PaidOfDice總和等於7時,我必須對它進行計數,直到循環終止於1000.所以我已經做了1000次1000循環的循環計數。我查看了輸出,並注意到當roller.roll()不等於7時,計數會遞增1。這是我的代碼。循環代碼運行良好,但它不會做我想做的事
import java.util.Scanner;
public class ChidoriDiceRoller
{
private PairOfDice roller;
private int count, sum;
public static void main(String[] args)
{
PairOfDice roller = new PairOfDice();
int count = 0;
for(int i = 1; i <= 1000; i++)
{
roller.roll();
System.out.println(i +"/"+ roller.roll() +"/"+ count);
if (roller.roll() == 7)
{
count++;
//System.out.println(roller.roll() +"/"+count);
}
}
}
}
/////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// /////////////////
//********************************************************************
// PairOfDice.java Author: Lewis/Loftus
//
// Solution to Programming Project 4.9 and 5.11
//********************************************************************
public class PairOfDice
{
private 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();
}
}
//////////////////////// ////////////////////////////////////////////////// /// ////////////////////////////////////////////// // ///////////////////////////////
//********************************************************************
// 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;
}
}
/// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// /////// 我剪掉了前900卷,但這裏是我的意思的一個例子。它不算正確。其中,這是 「I/roller.roll()/計數」,對於i =軋輥
900/10/142
901/3/142
902/10/143
903/9/143
904/9/143
905/10/143
906/4/143
907/9/144
908/6/144
909/11/144
910/10/144
911/6/144
912/4/144
913/8/144
914/7/145
915/7/145
916/6/145
917/6/145
918/9/145
919/7/145
920/10/145
921/8/145
922/12/145
923/6/145
924/5/145
925/7/145
926/5/146
927/7/146
928/6/146
929/11/146
930/5/146
931/10/146
932/12/146
933/6/146
934/4/146
935/6/146
936/5/146
937/7/146
938/2/146
939/8/146
940/2/146
941/3/146
942/8/146
943/11/147
944/9/147
945/5/147
946/11/147
947/9/147
948/5/147
949/9/147
950/9/147
951/5/147
952/6/147
953/6/147
954/7/147
955/6/147
956/8/147
957/4/147
958/9/147
959/8/147
960/7/147
961/6/147
962/8/147
963/3/147
964/11/147
965/4/147
966/6/147
967/7/147
968/10/147
969/4/147
970/10/147
971/6/147
972/7/147
973/10/147
974/6/147
975/10/147
976/8/147
977/9/148
978/8/148
979/9/148
980/3/148
981/11/148
982/10/148
983/10/148
984/11/148
985/7/148
986/6/148
987/6/148
988/10/148
989/7/148
990/10/148
991/9/148
992/4/148
993/4/148
994/9/148
995/7/148
996/11/148
997/9/149
998/8/149
999/5/149
1000/11/149
代碼中最後100個捲筒的示例。正如你可以看到計數不真正計數roller.roll()== 7我不知道爲什麼。 – silversk8terz