2015-11-06 52 views
0

我的目标是使用使用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 
+0

代码中最后100个卷筒的示例。正如你可以看到计数不真正计数roller.roll()== 7我不知道为什么。 – silversk8terz

回答

0

每次调用roller.roll()的时间的数目,它再次滚动骰子。

你应该把结果保存在一个变量,如

[...] 
int result = roller.roll() 
System.out.println(i +"/"+ result +"/"+ count); 

if (result == 7) 
{ 
    count++; 
    //System.out.println(roller.roll() +"/"+count); 
} 
[...] 

避免比你打算滚动多次。

+0

我刚刚更改了代码以使sum = roller.roll();并把它放在我的陈述中,但仍然给我错误的计数数字,我不知道为什么 – silversk8terz

+0

您是否在打印语句和if语句中使用了sum?当我运行这样的代码时,它可以正常工作。 – walter