本文共 864 字,大约阅读时间需要 2 分钟。
package Rolls;import Counter.Counter;import common.StdOut;import common.StdRandom;/** Copyright (C), 2020-2020, XXX有限公司 * FileName: Rolls * Author: cakin * Date: 2020/1/11 * Description: 模拟投掷骰子 */public class Rolls { public static void main(String[] args) { int T = Integer.parseInt(args[0]); int SIDES = 6; Counter[] rolls = new Counter[SIDES + 1]; for (int i = 1; i <= SIDES; i++) { rolls[i] = new Counter(i + "'s"); } for (int t = 0; t < T; t++) { int random = StdRandom.nextInt(SIDES) + 1; rolls[random].incrementCount(); } // 输出结果 for (int i = 1; i <= SIDES; i++) { StdOut.println(i + "'s count: " + rolls[i].getCount()); } }} 当投掷1000000次的结果如下:
166995 1's166249 2's166429 3's167018 4's166639 5's166670 6's
// 代码参考
转载地址:http://efqj.baihongyu.com/