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;
}
} 展开
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;
}
} 展开
2个回答
展开全部
你是在随机生成双色球的红球啊。。。
//定义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)];
//定义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)];
追问
不懂,烦请高手能逐句解释下吗,谢谢啊
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询