java程序解释,逐行解释,谢谢

packageoop_day01;importjava.util.Arrays;importjava.util.Random;publicclassDoubleBallD... package oop_day01;
import java.util.Arrays;
import java.util.Random;

public class DoubleBallDemo {
public static void main(String[] args) {
System.out.println(Arrays.toString(gen()));
}
public static String[] gen(){
String[] pool = {"01","02","03","04","05","06","07",
"08","09","10","11","12","13","14","15","16",
"17","18","19","20","21","22","23","24","25",
"26","27","28","29","30","31","32","33"};
boolean[] used = new boolean[pool.length];
Random random = new Random();
String[] balls = new String[6];
int i=0;
while(true){
int index = random.nextInt(pool.length);//[0,33)
if(used[index]){
continue;
}
balls[i++] = pool[index];
used[index]=true;
if(i==balls.length){
break;
}
}
Arrays.sort(balls);
balls = Arrays.copyOf(balls, balls.length+1);
balls[balls.length-1]=pool[random.nextInt(16)];
return balls;
}
}
展开
 我来答
elfasd
2013-10-09 · TA获得超过152个赞
知道小有建树答主
回答量:173
采纳率:100%
帮助的人:129万
展开全部
你是在随机生成双色球的红球啊。。。
//定义String类型的一维数组,01 - 33 即双色球的红球
String[] pool = {"01","02","03","04","05","06","07",
"08","09","10","11","12","13","14","15","16",
"17","18","19","20","21","22","23","24","25",
"26","27","28","29","30","31","32","33"};

//定义一个长度为32的boolean类型的数组,用于判断该随机数是否已经占用
boolean[] used = new boolean[pool.length];
//定义一个随机数生成类
Random random = new Random();
//定义用于最终存储6个红球的String数组
String[] balls = new String[6];
//定义下标
int i=0;

while(true){
//随机获取一个0-32的下标
int index = random.nextInt(pool.length);//[0,33)
//如果这个下标已经被占用了,跳出本次循环
if(used[index]){
continue;
}
//将该下标对应的那个红球的数山薯赋值给上面定义的数组
balls[i++] = pool[index];
//将该红球设置为占用,即已经稿碰摇出来了
used[index]=true;
//如果是最后一个球,跳出整个循环
if(i==balls.length){
break;
}
}
//键唯谈对结果进行排序
Arrays.sort(balls);
balls = Arrays.copyOf(balls, balls.length+1);
balls[balls.length-1]=pool[random.nextInt(16)];
追问
不懂,烦请高手能逐句解释下吗,谢谢啊
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
vindra
2013-10-09 · 超过11用户采纳过TA的回答
知道答主
回答量:42
采纳率:0%
帮助的人:30.4万
展开全部
汗 哪有人这么有时间帮你解释呀,你要学会看代码才行,看不懂的帮你解释
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式