A. 请问用java从1-33个整数中随机抽取6个数字 且不重复 1-16随机抽取一个数,给小球
完整代码为:
public class Main {
public static void main(String[] args) {
int index = 1;
int[] redBalls = new int[6];
Random random = new Random();
boolean getMoreRed = true;
boolean getAgain;
System.out.println("开始抽取红球!");
while (getMoreRed) {
getAgain = false;
int red = random.nextInt(36) + 1;
System.out.print("本次抽取到的红球为:[" + red + "]!");
for (int i = 0; i < index; i++) {
if (redBalls[i] == red) {
System.out.print("重复抽取,将重新抽取红球");
getAgain = true;
break;
}
}
System.out.println("");
if (getAgain){
continue;
}
redBalls[index - 1] = red;
index++;
getMoreRed = index < 7;
}
System.out.println("抽取到的红球为:");
Arrays.sort(redBalls);
for (int redBall : redBalls) {
System.out.print(redBall + " ");
}
System.out.println("
开始抽取蓝球!");
System.out.println("本次抽取到的蓝球为:[" + (random.nextInt(16) + 1) + "]!");
}
}