本人偶尔会去买注双色球,使自己不致于失去最后一丝发财的梦想。~~
选择号码是个麻烦的事情。于是乎用JAVA写了一个双色球随机号程序。代码如下:
package com.mainone.util;
import java.util.ArrayList;import java.util.List;import java.util.Random;
/** * @author 武广阔
* @date:2011-1-17 上午10:13:08 * @version : * */public class CoupleColorBall {
/** * @param args */ public static void main(String[] args) { List<Integer> redBall = new ArrayList<Integer>(); for(int i=1; i<=33; i++){ redBall.add(new Integer(i)); } List<Integer> blueBall = new ArrayList<Integer>(); for(int i=1; i<=16; i++){ blueBall.add(new Integer(i)); } List<Integer> redResult = new ArrayList<Integer>(); for(int i=0; i<6; i++){ try { Thread.sleep(15000); Random rd = new Random(); Integer tmp = rd.nextInt(redBall.size()); redResult.add(redBall.get(tmp)); redBall.remove(tmp); } catch (InterruptedException e) { e.printStackTrace(); } } try { Thread.sleep(20000); } catch (InterruptedException e) { e.printStackTrace(); } Integer blueResult = blueBall.get(new Random().nextInt(16)); System.out.println(redResult); System.out.println(blueResult); }
}
一开始运行是没有问题的,后来用我的宏基本本(CPU:I3 370M)运行,把Thread.sleep后边数值修改得大了一些,在redResult中发现了重复的数字。找问题呗,找不到问题出在哪里。于是乎就开始猜想:是不是JDK在我的双核四线程CPU上运行Thread.sleep会出问题。
于是就煞费苦心地去Oracle官网上提交了一个BUG,又煞费苦心地在上边发表了一篇英文帖子。
还好有人能看懂我发的英文帖子,还给回复了——我的英文还不是太烂哦,哈哈!。
发现问题出在这里:redBall.remove(tmp);这里的tmp是Integer类型的,remove接收一个Object类型时,会到list里删除这个Object,而这里真正的意图是,删除掉指定索引对应的对像。将tmp强制转换成int型即可。!==,这下丢人丢大发了,直接丢出国门了。@@.
看来以后发现问题,还得多从自身找原因啊。
最后欢迎各位使用本程序或修改后的程序去购买彩票。——在程序员的道路上共同进步。
路漫漫其修远兮,吾将上下而求索。
