java 随机数 模拟双色球开奖

买了好几期彩票居然连续中了45人民币,相对5块钱都没中过的同学,运气应该还算好的吧 ,呵呵, 每次都是去随机的。觉得还是自己来随机有意义些。顺便来复习一下java基础。
本程序很简单,就用到一个随机数和追加写文件,随机生成指定个双色球号保存到指定文件。代码如下:

package com.maobo.action;

import java.io.*;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;

/**
 * ---------------------------------------------------------------------------
 * Copyright (c) 2015, [email protected] All Rights Reserved.
 * Project  ssq
 * File ShuanSeQiu
 * Author   bo.mao
 * Date:2015/8/12.
 * ---------------------------------------------------------------------------
 */
public class ShuanSeQiu
{

    public static String createqq()
    {
        Sphere sphere = new Sphere();
        Set redSphereNumberSet = new TreeSet<>();
        Random random = new Random();
        //生成红球1-33
        while ( true ) {
            if ( redSphereNumberSet.size() < 6 ) {
                int red = random.nextInt ( 32 );
                redSphereNumberSet.add(red + 1);
            } else {
                break;
            }
        }
        sphere.setRedSphere ( redSphereNumberSet );
        //生成篮球1-16
        int blue = random.nextInt ( 15 );
        sphere.setBlueSphere ( blue + 1 );
        return sphere.toString();
    }

    public static void writeFile(String content)
    {
        BufferedWriter out = null;
        String filePath = "d:/ssq_data.txt";

        try {
            out = new BufferedWriter ( new OutputStreamWriter (new FileOutputStream ( filePath, true ) ) );
            out.write ( content + "\r\n" );
        } catch ( Exception e ) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main ( String[] args )
    {
        for ( int i = 0; i < 100; i++ ) {
            String ssq = createqq();
            writeFile(ssq);
        }
    }
}

class Sphere
{
    Set redSphere;
    int blueSphere;

    public Set getRedSphere()
    {
        return redSphere;
    }

    public void setRedSphere ( Set redSphere )
    {
        this.redSphere = redSphere;
    }

    public int getBlueSphere()
    {
        return blueSphere;
    }

    public void setBlueSphere ( int blueSphere )
    {
        this.blueSphere = blueSphere;
    }

    public String toString()
    {
        String sphereNumber = "";
        Iterator it = redSphere.iterator();

        for ( Integer i : redSphere ) {
            sphereNumber = sphereNumber + "\t" + i + "\t";
        }

        sphereNumber = sphereNumber + "\t" + blueSphere;
        return sphereNumber;
    }


}


随着知识的进步和工作经验的积累此程序还可以不断优化吧,与其看电视娱乐不如写写博客搞搞复习!不断提高自己综合竞争力!
2015年8月12日 不早了 晚安 早睡早起~

—————————–2017年9月2日11:57:03————
lm优化版2.0

 package com.mb.test;
import java.util.Random;
import java.util.Set;
import com.mb.po.Qiu;
public class LiuHeCai {

    public static void main(String[] args) {
      Qiu qiu = queryQiu();
      System.out.println(qiu);
    }

    private static Qiu queryQiu() {
        Qiu qiu = new Qiu();
        Random random = new Random();
        Set red = qiu.getRed();

        while(red.size()<6){
            red.add(random.nextInt(32)+1);
        }
        qiu.setBlue(random.nextInt(15)+1);
        return qiu;
    }
}

package com.mb.po;

import java.util.Set;
import java.util.TreeSet;

public class Qiu {
    private Set red = new TreeSet<>();
    private Integer blue;

    public Set getRed() {
        return red;
    }
    public void setRed(Set red) {
        this.red = red;
    }
    public Integer getBlue() {
        return blue;
    }
    public void setBlue(Integer blue) {
        this.blue = blue;
    }
    @Override
    public String toString() {
        return "Qiu [red=" + red + ", blue=" + blue + "]";
    }
}

你可能感兴趣的:(java)