这两个类之间的逻辑区别是什么?
What is the logical difference between these two classes?
我写了一个Java class来解决教科书Javanotes 7中的一个问题,它要求一个程序来显示与滚动次数相关的统计信息需要在一对骰子上获得给定值。我的 class 没有给我正确的统计数据,但据我所知,它在逻辑上与教科书中给出的解决方案相同。显然不是。
这是我的代码:
/**
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
我看不出它们之间的逻辑区别。这是什么?
您正在通过初始化 int counter = 1;
计算 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 循环得到一堆迭代,计数 == 1。基本上,掷骰子是这种方式没有正确模拟。
我写了一个Java class来解决教科书Javanotes 7中的一个问题,它要求一个程序来显示与滚动次数相关的统计信息需要在一对骰子上获得给定值。我的 class 没有给我正确的统计数据,但据我所知,它在逻辑上与教科书中给出的解决方案相同。显然不是。 这是我的代码:
/**
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
我看不出它们之间的逻辑区别。这是什么?
您正在通过初始化 int counter = 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 循环得到一堆迭代,计数 == 1。基本上,掷骰子是这种方式没有正确模拟。