我已经编写了一个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 :(得分:0)
您正在通过初始化int counter = 1;
尝试用0初始化它。
答案 1 :(得分:0)
int rollCt = 0; // Number of rolls made.
do {
dice.roll();
rollCt++;
} while ( dice.getDie1() + dice.getDie2() != N );
return rollCt;
这里的骰子在do ... while循环中的布尔测试之前滚动。
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循环被跳过 - 并且你通过for循环得到一堆迭代,其中count == 1.基本上,骰子卷是这种方式不能很好地模拟。