标签:des style blog http color ar java for sp
对象的数组:模拟T次投掷骰子的Counter对象的用例:
Rolls.java 如下:所用类列表:Counter, StdOut, StdRandom 见page42
public class Rolls{ public static void main(String[] args){ int T = Integer.parseInt(args[0]);//转String为int 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=1;t<=T;t++){ int result = StdRandom.uniform(1,SIDES+1); //去随机数, 1-7 rolls[result].increment();//给1-7号元素自增1 } for(int i=1;i<=SIDES;i++) StdOut.println(rolls[i]); } }
打印结果:
标签:des style blog http color ar java for sp
原文地址:http://www.cnblogs.com/pacoson/p/4003523.html