永发信息网

双色球排序选号的java程序,老师讲过的,为什么又出错呢?

答案:3  悬赏:30  手机版
解决时间 2021-02-19 22:53
  • 提问者网友:呐年旧曙光
  • 2021-02-19 03:23
eclipse里运行时balls = Arrays.copyOf(balls, balls.length+1);这行有错误。
以下为全部代码,求大神指点。
package unix.day01;
import java.util.Arrays;
import java.util.Random;
public class DoubleBallDemo {
public static void main(String[] args) {
String[] ticket = gen();
System.out.println(Arrays.toString(ticket));
}
public static String[] gen(){
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"};
boolean[] used = new boolean[pool.length];
String[] balls = new String[6];
Random random = new Random();
int i=0;
while(true){
int index = random.nextInt(pool.length);
if(used[index]){
continue;//返回重新执行循环
}
balls[i++]=pool[index];
used[index]=true;//index对应的球被取出使用了!
if(i==balls.length){
break;//如果数量够了, 就结束循环
}
}
Arrays.sort(balls);
balls = Arrays.copyOf(balls, balls.length+1);
balls[balls.length-1] = pool[random.nextInt(16)];
return balls;
}
}

求大神看看啊,是不是跟jdk版本有关?我百度搜了下,说是jdk1.6版本的Arrays才有copyOf方法,1.5版本的没有。。
最佳答案
  • 五星知识达人网友:动情书生
  • 2021-02-19 03:46
代码是对的啊,在我这里运行成功的,你的报错是什么,贴来看看。
全部回答
  • 1楼网友:神鬼未生
  • 2021-02-19 05:17
太有才了,不过貌似双色球出号码不是按大小顺序来的
  • 2楼网友:思契十里
  • 2021-02-19 04:42
没有错误运行正常,不知道 balls = Arrays.copyOf(balls, balls.length+1);这句话没用吧。。。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯