- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.List;
- import java.util.Scanner;
- public class DoubleBall{
- private Integer[] redNumPool = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
- 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33};
- private Integer[] blueNumPool = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,16};
- private int choiceCount = 7;
- /**
- * 随机选择一注
- * @return
- */
- private int[] choiceNums(){
- List<Integer> tempNumsList = new ArrayList<Integer>(
- Arrays.asList(redNumPool));
- //所选号码
- int[] selNums = new int[choiceCount];
- //选择红球
- int tempTotalLength = redNumPool.length;
- for (int j = 0; j < choiceCount-1; j++) {
- int index = this.randomChoice(tempTotalLength);
- selNums[j] = tempNumsList.get(index);
- //每选择一个就删除该元素
- tempNumsList.remove(index);
- tempTotalLength--;
- }
- //选择蓝球
- selNums[choiceCount - 1] = this.choiceBlueNum();
- //蓝球不排序
- Arrays.sort(selNums, 0, choiceCount - 1);
- return selNums;
- }
- /**
- * 选择蓝球
- * @return
- */
- private int choiceBlueNum(){
- int index = this.randomChoice(blueNumPool.length);
- return blueNumPool[index];
- }
- /**
- * 随机选择
- * @param count
- * @return
- */
- private int randomChoice(int count){
- return (int) (Math.random() * count);
- }
- /**
- * 选择一次,返回中奖所需次数
- * @param expectNums
- * @return
- */
- private int doChoice(int[] expectNums){
- int count = 0;
- while (!Arrays.equals(this.choiceNums(), expectNums)) {
- count++;
- }
- return count;
- }
- /**
- * 获取用户输入的投注
- * @return
- */
- private int[] getUserInput(){
- Scanner sc = new Scanner(System.in);
- int[] selNums = new int[choiceCount];
- List<Integer> selList = new ArrayList<Integer>();
- String regBlue = "(3[0-3]|2[0-9]|1[0-9]|[1-9])"; // 1-33
- sc.useDelimiter("\\\\s");
- while(sc.hasNext(regBlue)){
- selList.add(sc.nextInt());
- }
- int size = selList.size();
- if(selList.isEmpty()){
- return this.choiceNums();
- }else if(size != choiceCount || selList.get(choiceCount-1) > 16){
- System.out.println("所选位数不够或蓝球数太大,自动为你机选");
- return this.choiceNums();
- }
- for(int i=0;i<size;i++){
- selNums[i] = selList.get(i);
- }
- return selNums;
- }
- /**
- * 开始测试
- */
- private void play() {
- String flag = "";
- while(!flag.equals("n")){
- System.out.println("请输入所选双色球,以空格分隔,最后一位为蓝球。如果随机选择直接回车。。");
- int testCount = 5; // 测试次数
- int[] userInputNums = this.getUserInput();
- if (userInputNums == null || userInputNums.length == 0) {
- userInputNums = this.choiceNums();
- }
- System.out.println("你所选的双色球号码为:" + Arrays.toString(userInputNums)
- + ",正在开奖,请稍后。。。");
- // 多次测试求平均值
- int totalCount = 0;
- int minCount = 0;
- int maxCount = 0;
- for (int i = 0; i < testCount; i++) {
- long startTime = System.currentTimeMillis();
- int count = this.doChoice(userInputNums);
- long endTime = System.currentTimeMillis();
- if(i == 0){
- minCount = maxCount = count;
- }else if(count > maxCount){
- maxCount = count;
- }else if(count < minCount){
- minCount = count;
- }
- totalCount += count;
- System.out.println("第" + (i+1) + "次尝试,所需次数:"
- + count + "。CPU指数:" + count * 10 / (endTime - startTime)
- + ",打败了85%的机友!"
- + "\\n\\t恭喜你!如果每天买一注,在" + this.getWinDate(count)
- + "那天你就可以获奖啦啦啦~~~");
- }
- System.out.println("平均次数:" + totalCount / testCount + ",\\n\\t你最可能在"
- + this.getWinDate(totalCount / testCount) + "这天中奖!");
- System.out.println("最少次数:" + minCount + "\\n\\t恭喜你!你最早在"
- + this.getWinDate(minCount) + "这天中奖!打败了83%的网友。吖灭蝶~~!");
- System.out.println("最多次数:" + totalCount / testCount + ",\\n\\t你最晚会在"
- + this.getWinDate(maxCount) + "这一天中奖!");
- System.out.println("是否继续? (y/n)");
- Scanner sc = new Scanner(System.in);
- flag = sc.next();
- }
- }
- /**
- * 获取中奖日期
- */
- public String getWinDate(int day) {
- Calendar c = Calendar.getInstance();
- c.setTime(new Date());
- c.setTimeInMillis(c.getTimeInMillis() + ((long) day) * 24 * 3600 * 1000);
- return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
- }
- public static void main(String[] args){
- new DoubleBall().play();
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/0206201512731.html
来源: http://www.codesnippet.cn/detail/0206201512731.html