import java.util.*;
public class doublecolourball {
public static void main(String[] args) {
System.out.println(Arrays.toString(randomBall()));//Arrays在java.util.*,里面
}
public static String[] randomBall(){
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"};
String[] balls=new String[7];//字符串型,因球是01,02,03......33
boolean[] flags=new boolean[33];//数组默认元素值为“0”,布尔型为false
int i=0;
Random r=new Random();//用到类库中函数Random
while(true){
int index=r.nextInt(33);//随机的33个,指的是[0,33),即索引或下标从0到32,不包括33,刚好33个
if(flags[index]==true){
continue;//表示这号被选过了,跳过
}
balls[i++] = pool[index];//把没选过的存放到balls数组中,i
flags[index] = true; if(i==6){
break; //当找到6个时候停止
}
}
Arrays.sort(balls,0,5);//Arrays.sort(canshu)系统对数组的排序算法,0,5,表示排序0-5,
//int index=r.nextInt(16);//同上,[0,16)
//String blueBall=pool[index];//蓝球就1个字符,所以只需随机一个放进blueBall中
//balls[6]=blueBall;
balls[6] = pool[r.nextInt(16)];//这是对上面三条的简化,把蓝球buleBall放入到balls中第7个,即下标6那
return balls;
}
}
//下面是对于flag[index]==false帮助理解
//(flag[index]==false)==true;(falg[index]==true)==false
//if(true)== if(!flag[index])==if((flag[index]==false))
//if(false)==if(flag[index]) == if(falg[index]==true)
转载于:https://blog.51cto.com/wudi6/1683696