利用数组,
随机生成五组的双色球
红(1-32) 6位 绿(1-16) 1位
代码一:
/
public class G3 {
public static void main(String[] args) {
//双色球
Random random = new Random();
int[] reds = new int[6];
for (int i = 0; i < reds.length; i++) {
int sr = random.nextInt(1, 32);
boolean b = true;
for (int j = 0; j < reds.length; j++) {
if (reds[j] == sr) {
i--;
b = false;
break;
}
}
if (b) {
reds[i] = sr;
}
}
Arrays.sort(reds);
System.out.println("\033[31m" + Arrays.toString(reds) + "\033[0m");
int[] blue = new int[]{random.nextInt(1, 17)};
System.out.println("\033[32m" + Arrays.toString(blue) + "\033[0m");
}
}
代码二:
public class F4 {
public static void main(String[] args) {
F4 SS=new F4();
System.out.println("\033[31m"+Arrays.toString(SS.getNums(6))+"\033[0m");
System.out.println("\033[32m"+Arrays.toString(SS.getNum(1))+"\033[0m");
}
public int[] getNums(int len){
int [] tt =new int[len];
Random random = new Random();
for (int i = 0; i < len; i++) {
tt[i]= random.nextInt(1,31);
}
return tt;
}
public int[] getNum(int len){
int [] tt =new int[len];
Random random = new Random();
for (int j = 0; j < len; j++) {
tt[j] = random.nextInt(1, 17);
}
return tt;
}
}