一道Java题目:一项抽奖活动要求你从整数1-49之间选择6个不同的数字,编写一个程序来完成这项工作,并生成5组

来源:百度知道 编辑:UC知道 时间:2024/06/15 07:11:29
菜鸟请求各位大虾帮忙

用Set和Random,就OK了,简单,不懂你把这两个类看看,嘎嘎

class TakeOutAward {

TakeOutAward() {
numbers = new int[6];
}

void takeOut() {
int count = 6;
while(count > 0) {
int rand = (int)(Math.random() * 48 + 1);
switch(count) {
case 6:
numbers[0] = rand;
count--;
break;
case 5:
if(numbers[0] == rand) {
continue;
}
numbers[1] = rand;
count--;
break;
case 4:
if(numbers[0] == rand || numbers[1] == rand) {
continue;
}
numbers[2] = rand;
count--;
break;
case 3:
if(numbers[0] == rand || numbers[1] == rand || numbers[2] == rand) {
continue;
}
numbers[3] = rand;
count--;
break;
case 2:
if(numbers[0] == rand || numbers[1] == rand || numbers[2] == rand || numbers[3] == rand)