【发布时间】:2015-09-20 01:46:30
【问题描述】:
我正在编写一个掷骰子程序。该代码可以很好地计算出频率的面和数量。如何计算每张脸的频率百分比?
import java.util.Random;
public class Dice{
public static void main (String[] args){
Random random = new Random(); // Generates random numbers
int[] array = new int[ 7 ]; // Declares the array
//Roll the die 10000 times
for ( int roll = 1; roll <=10000; roll++ ) {
/*++array[1 + random.nextInt(6)];*/
int dice = 1 + random.nextInt(6);
++array[dice];
}
System.out.printf("%s%10s\n", "Face", "Frequency");
// outputs array values
for (int face = 1; face < array.length; face++) {
System.out.printf("%4d%10d\n", face, array[face]);
}
}
}
【问题讨论】:
-
从你的问题我怀疑这不是你的代码......你知道如何计算一般的百分比吗?你试过什么?
标签: java