java双色球

数组方法

import java.util.Random;

public class DoubleColorRandom {

    public static void main(String[] args) {
        Random r = new Random();
        int[] redNumber = { 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 };
        int[] redNum = new int[6];
        int a = 0;
        int b = 0;
        System.out.println("红球号码为:");
        for (int i = 0; i < redNum.length; i++) {
            //
            while (true) {
                a = r.nextInt(33);
                System.out.print(a+" ");
                //判断是否重复取数
                if (redNumber[a] != 0) {
                    redNum[i] = redNumber[a];
                    redNumber[a] = 0;
                    System.out.print(redNum[i] + "\t");
                    break;
                }
            }
        }
        // 排序
        for (int i = 6; i > 0; i--) {
            for (int j = 1; j < i; j++) {
                if (redNum[j - 1] > redNum[j]) {
                    redNum[j - 1] = redNum[j - 1] + redNum[j];
                    redNum[j] = redNum[j - 1] - redNum[j];
                    redNum[j - 1] = redNum[j - 1] - redNum[j];
                }
            }

        }
        System.out.println();
        System.out.println("排序输出红球号码:");
        for (int i = 0; i < redNum.length; i++) {
            System.out.print(redNum[i] + "\t");
        }

        System.out.println();

        b = r.nextInt(16) + 1;// 随机得到篮球号码
        System.out.println("蓝球号码为:" + "\t" + b);

    }

}

集合

package com.newer.cn;

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

public class DoubleBalls {

    public static void main(String[] args) {
        //实例化球池
        ArrayList<String> pools = new ArrayList<String>();
        //创建集合保存从球池中取出的球
        ArrayList<String> balls = new ArrayList<String>();
        //产生随机数
        Random random = new Random();
        for(int i = 1;i <= 33;i++){
            if(i < 10){
                pools.add("0"+i);
            }else{
                pools.add(""+i);
            }
        }
        System.out.println(pools);
        //取红球
        int randNum = 0;
        for(int i = 0;i < 6;i++){
            randNum = random.nextInt(pools.size());
            balls.add(pools.get(randNum));
            pools.remove(randNum);
        }
        System.out.println(balls);
        System.out.println(pools);
    }

}

 

posted @ 2017-03-15 19:50  摩西摩西点点  阅读(223)  评论(0编辑  收藏  举报