标题:判断是否重复报错
只看楼主
兜兜里有耗子
Rank: 2
等 级:论坛游民
帖 子:16
专家分:18
注 册:2012-10-8
结帖率:100%
已结贴  问题点数:20 回复次数:6 
判断是否重复报错
模仿双色球前6位

package balls;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class balls {
    public static void main(String[] args) {
        List list=new ArrayList();
        for (int i = 1    ; i <= 33; i++) {
            list.add(i);   
            //System.out.println(list);
        }
        List balls=new ArrayList();
        Random random=new Random();
        
        for (int i = 0; i < 6; i++) {
            int r=random.nextInt(list.size());
            balls.add(list.get(r));
            for(int j=0;j<6;j++){
                if(list.get(r)==balls.get(j)){//现在得到的球和原来的数字对比不重复,此行代码报错
                    balls.remove(i);i--;
                }
            }
            System.out.println(balls);
        }
   
   
    }

}
搜索更多相关主题的帖子: 双色球 package public Random 
2012-12-02 10:46
烧包谷
Rank: 2
等 级:论坛游民
威 望:2
帖 子:60
专家分:69
注 册:2010-10-11
得分:7 
Random random=new Random();不能这样做,Random对象生成的随机数是伪随机数,是用算法产生满足某一规律的,你要产生不相同的随机数应该在Random对象中添加种子。Random random = new Random((new Date()).getTime());否则产生的随机数不能满足要求,会提示无穷递归的异常。
2012-12-02 18:50
烧包谷
Rank: 2
等 级:论坛游民
威 望:2
帖 子:60
专家分:69
注 册:2010-10-11
得分:8 
  for (int i = 0; i < 6; i++) {
             int r=random.nextInt(list.size());
             balls.add(list.get(r));//i=0时,这是第一次添加,集合中只有一个元素
             for(int j=0;j<6;j++){
                 if(list.get(r)==balls.get(j)){//i=0时,balls就一个元素,j的取值不仅是0,还有1,2,3,4,5
                     balls.remove(i);i--;//
                 }
             }
             System.out.println(balls);
         }
2012-12-02 19:20
烧包谷
Rank: 2
等 级:论坛游民
威 望:2
帖 子:60
专家分:69
注 册:2010-10-11
得分:5 
验证最好用递归来做,这种循环你很难控制!
2012-12-02 19:20
兜兜里有耗子
Rank: 2
等 级:论坛游民
帖 子:16
专家分:18
注 册:2012-10-8
得分:0 
回复 4楼 烧包谷
好的谢谢~
2012-12-05 19:45
heqing891016
Rank: 1
等 级:新手上路
帖 子:3
专家分:2
注 册:2012-2-20
得分:0 
给楼主看看我以前写的类似的双色球代码
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"}; //pool 池
    boolean[] used = new boolean[pool.length];
    Random random = new Random();
    String[] balls = new String[6];//{^,^,^,^,^,^}
    int i=0;
    for(;;){//while(true) == for(;;)
      int index = random.nextInt(pool.length);
      if(used[index]){ continue; }
      balls[i++] = pool[index];
      used[index]=true;
      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;
  }
}
2012-12-18 23:54
shellingford
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:19
帖 子:228
专家分:1348
注 册:2010-8-9
得分:0 
回复 2楼 烧包谷
public Random() { this(++seedUniquifier + System.nanoTime()); }

无参数的构造函数也被java加上了系统时间变量
2012-12-19 09:30



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-389267-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.140123 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved