62,628
社区成员
发帖
与我相关
我的任务
分享public class Lottery {
public static void main(String[] art){
int[] re=aLottery(33,7);
for(int i=0;i<=re.length;i++){
System.out.println(re[i]);}
}
public static int[] aLottery(int max,int count){
int[] result=new int[count];
Random r=new Random();
result[0]=r.nextInt(max);
for(int i=0;i<=count;i++){
boolean flag=true;
int temp=r.nextInt(max);
for(int j=0;j<i;j++){
if(temp==r.nextInt(j)){
flag=false;
break;
}
if(flag==true){
result[i]=temp;
}
}
}
return result;
}
}

public class Lottery {
public static void main(String[] art) {
int[] re = aLottery(33, 7);
for (int i = 0; i < re.length; i++) {
System.out.println(re[i]);
}
}
public static int[] aLottery(int max, int count) {
int[] result = new int[count];
Random r = new Random();
result[0] = r.nextInt(max);
for (int i = 0; i < count; i++) {
boolean flag = true;
int temp = r.nextInt(max);
for (int j = 0; j < i; j++) {
if (j != 0 && (temp == r.nextInt(j))) {
flag = false;
break;
}
if (flag == true) {
result[i] = temp;
}
}
}
return result;
}
}
两个问题 一个是nextInt(),另外一个是数组遍历下标。



