2015-01-08 71 views
0

我已經編寫了一個Java類來解決教科書Javanotes 7中的一個問題,該教程要求提供一個程序,該程序顯示與在擲骰子上獲得給定值所需的擲骰數量相關的統計信息。我的班級沒有給我正確的統計數據,但據我所知,這在教科書中給出的解決方案在邏輯上是相同的。顯然不是。 這裏是我的代碼:這兩個類的邏輯區別是什麼?

/** 
    This program rolls a pair of dice until they come up a certain value. It repeats this for a certain number of trials and then gives the user the average number of 
    rolls required to achieve the target value. It does this for each possible value of two six-sided dice. It also gives the standard deviation and the maximum 
    number of rolls. 
*/ 

public class DiceAverage{ 

    static final int SAMPLE_SIZE = 10000; 

    public static void main(String[] args){ 

     System.out.println("Total on Dice Average Number of Rolls Standard Deviation Maximum Number of Rolls"); 
     System.out.println("------------- ----------------------- ------------------ -----------------------"); 

     //each dice value iterator 
     for(int i = 2; i < 13; i ++){ 

      //for each value, create a Statcalc, and PairOfDice object 
      StatCalc dataset = new StatCalc(); 
      PairOfDice dice = new PairOfDice(); 

      //each trial iterator 
      for(int j = 0; j < SAMPLE_SIZE; j ++){ 

       int counter = 1;//counter for number of rolls. Initialized at 1 because dice object is rolled upon construction. 

       //get die1 and die2 
       while(dice.getDie1() + dice.getDie2() != i){ 

        dice.roll(); 
        counter ++; 

       } 

       dataset.enter(counter); 

      } 

      System.out.printf("  %-19d%-25.3f%-25.3f%1.3f%n", i, dataset.getMean(), dataset.getStandardDeviation(), dataset.getMax()); 

     } 

    } 

} 

這裏是實際的解決方案:

/**This program performs the following type of experiment: 
* Given a desired total roll, such as 7, roll a pair of 
* dice until the given total comes up, and count how many 
* rolls are necessary. Now do the experiment over and over, 
* and find the average number of rolls. The number of times 
* the experiment is repeated is given by the constant, 
* NUMBER_OF_EXPERIMENTS. Several statistics are computed and 
* printed out for each possible roll = 2, 3, ..., 12: 
* the average number of rolls, the standard deviation, 
* and the maximum number of rolls. 
*/ 

public class DiceRollStats2 { 

    static final int NUMBER_OF_EXPERIMENTS = 10000; 

    private static PairOfDice dice = new PairOfDice(); 
      // A single pair of dice, which will be used for all 
      // the experiments. 


    public static void main(String[] args) { 

     System.out.println("Dice Total Avg # of Rolls Stand. Deviation Max # of Rolls"); 
     System.out.println("---------- -------------- ---------------- --------------"); 

     for (int total = 2; total <= 12; total++) { 
      StatCalc stats; // An object that will compute the statistics. 
      stats = new StatCalc(); 
      for (int i = 0; i < NUMBER_OF_EXPERIMENTS; i++) { 
       // Do the experiment of counting the number of rolls 
       // required to roll the desired total, and enter the 
       // number of rolls into stats' dataset. 
      stats.enter(rollFor(total)); 
      } 
      System.out.printf("%6d", total); 
      System.out.printf("%18.3f", stats.getMean()); 
      System.out.printf("%19.3f", stats.getStandardDeviation()); 
      System.out.printf("%14.3f", stats.getMax()); 
      System.out.println(); 
     } 

    } // end main 

    /** 
    * Roll the dice repeatedly until the total on the 
    * two dice comes up to be N. N MUST be one of the numbers 
    * 2, 3, ..., 12. (If not, this routine will go into an 
    * infinite loop!). The number of rolls is returned. 
    */ 
    static int rollFor(int N) { 
     int rollCt = 0; // Number of rolls made. 
     do { 
      dice.roll(); 
      rollCt++; 
     } while (dice.getDie1() + dice.getDie2() != N); 
     return rollCt; 
    } 


} // end class DiceRollStats2 

我看不到它們之間的邏輯差異。它是什麼?

回答

0
int rollCt = 0; // Number of rolls made. 
    do { 
     dice.roll(); 
     rollCt++; 
    } while (dice.getDie1() + dice.getDie2() != N); 
    return rollCt; 

這裏的骰子在do ... while循環布爾測試之前推出初始化它計數加1卷。

int counter = 1;//counter for number of rolls. Initialized at 1 because dice object is rolled upon construction. 

      //get die1 and die2 
      while(dice.getDie1() + dice.getDie2() != i){ 

       dice.roll(); 
       counter ++; 

      } 

雖然這裏的骰子在布爾測試後滾動。因此,如果骰子等於i,那麼擲骰子值永遠不會被roll()改變 - 因爲while循環被跳過了 - 並且您通過count == 1的for循環獲得了一堆迭代。基本上,擲骰子是沒有用這種方式正確模擬。

0

你被初始化int counter = 1;

嘗試用0

+0

構造PairOfDice對象時會自動滾動。這就是爲什麼我將計數器初始化爲1。 – AlexanderDickinson