彩票之–超级大乐透选号小程序
是不是每次买彩票的时候,总是不知道选什么号码好呢?今天来看看这个小程序吧!
package Thread;
import java.util.*;
/*超级大乐透选号机*/
public class lottery {
public static void main(String[] args) {
//因为彩票号码不可重复,所以利用Set集合作为容器
Set<Integer> listA=new HashSet<>();
HashSet<Integer> listB = new HashSet<>();
//创建随机数
Random r=new Random();
int a;
int b;
/*红区有五个号码*/
while (listA.size()<5){
a=r.nextInt(36);//蓝区号码为:1-35
if (a>0){
listA.add(a);
}
}
/*蓝区只有两个号码*/
while (listB.size()<2){
b=r.nextInt(13);//后区号码为:1-12
if (b>0){
listB.add(b);
}
}
//将集合转为数组
Object[] red =listA.toArray();
Object[] blue = listB.toArray();
Arrays.sort(red);//排序
Arrays.sort(blue);//排序
/*利用数组工具进行打印输出(除char类型数组外,其他类型数组都需要借用数组类工具进行打印)*/
System.out.println(Arrays.toString(red)+"+"+Arrays.toString(blue));
}
}
看一下运行效果:
哈哈,代码逻辑是不是很简单呢?如有漏洞,欢迎留言!