近期一直在复习Java基础知识,确实不仅补缺查漏,还能增强记忆。同事提了个需求想要模拟一下双色球的输出,顺便练习一下刚学的Collection类。反正闲的没事,就决定练习一下。
一开始我想用Set最为简便,因为其自身值的唯一性,可以很好地去除随机值重复的问题。所以,一会儿就用Set写了下面这个版本。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
/* *Create on Aug 20, 2012 * */ package com.doubleball; import java.util.HashSet; import java.util.Random; import java.util.Set; /** * @author zhiguo.chen * */ public class Ball { Set<Integer> RedBall = null; public Set<Integer> getRedBall() { return RedBall; } public void setRedBall(Set<Integer> redBall) { RedBall = redBall; } Set<Integer> BlueBall = null; public Set<Integer> getBlueBall() { return BlueBall; } public void setBlueBall(Set<Integer> blueBall) { BlueBall = blueBall; } public Set getRedBall(int x,int y){ Random ran = new Random(); Set<Integer> tmp = new HashSet<Integer>(); for(int i = 0;tmp.size()<x;i++){ tmp.add(ran.nextInt(y)); } this.RedBall=tmp; return RedBall; } public Set getBlueBall(int x,int y){ Random ran = new Random(); Set<Integer> tmp = new HashSet<Integer>(); for(int i = 0;tmp.size()<x;i++){ tmp.add(ran.nextInt(y)); } this.BlueBall=tmp; return BlueBall; } } |