提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
滚动六面模具6000次。
import java.security.SecureRandom;/*来自 n o w j a v a . c o m*/ public class Main { public static void main(String[] args) { // randomNumbers object will produce secure random numbers SecureRandom randomNumbers = new SecureRandom(); int frequency1 = 0; // count of 1s rolled int frequency2 = 0; // count of 2s rolled int frequency3 = 0; // count of 3s rolled int frequency4 = 0; // count of 4s rolled int frequency5 = 0; // count of 5s rolled int frequency6 = 0; // count of 6s rolled//来 自 N o w J a v a . c o m // tally counts for 6,000,000 rolls of a die for (int roll = 1; roll <= 6000000; roll++) { int face = 1 + randomNumbers.nextInt(6); // number from 1 to 6 // use face value 1-6 to determine which counter to increment switch (face) { case 1: ++frequency1; // increment the 1s counter break; case 2: ++frequency2; // increment the 2s counter break; case 3: ++frequency3; // increment the 3s counter break; case 4: ++frequency4; // increment the 4s counter break; case 5: ++frequency5; // increment the 5s counter break; case 6: ++frequ