[Java] 纯文本查看 复制代码
public class HashSetTest07 {
public static void main(String[] args) {
// 创建双色求数组
String[] color = { "红", "蓝" };
// ArrayList集合
ArrayList<String> ball = new ArrayList<>();
// 添加红球
for (int j = 1; j <= 33; j++) {
ball.add(color[0] + j);
}
//添加篮球
for(int i = 1;i<=16;i++) {
ball.add(color[1] +i);
}
//创建Random对象
Random r = new Random();
//定义新集合接收双色球号码
ArrayList<String> newBall = new ArrayList<>();
//取出六个红球
//定义计数器
int count = 0;
while(count<6) {
int index = r.nextInt(ball.size());
if(ball.get(index).startsWith("红")) {
newBall.add(ball.get(index));
ball.remove(index);
count++;
}
}
//取出一个蓝球
count = 0;
while(count == 0) {
int index = r.nextInt(ball.size());
if(ball.get(index).startsWith("蓝")) {
newBall.add(ball.get(index));
ball.remove(index);
count++;
}
}
System.out.println(newBall);
}
}