java 随机数 模拟双色球开奖

来源:互联网 发布:免费网络在线代理 编辑:程序博客网 时间:2024/05/16 23:38

买了好几期彩票居然连续中了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, maobo@live.cn- 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<Integer> redSphereNumberSet = new TreeSet<>();        Random random = new Random();        //生成红球1-33        while ( true ) {            if ( redSphereNumberSet.size() < 6 ) {                int red = random.nextInt ( 33 );                redSphereNumberSet.add(red + 1);            } else {                break;            }        }        sphere.setRedSphere ( redSphereNumberSet );        //生成篮球1-16        int blue = random.nextInt ( 16 );        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<Integer> redSphere;    int blueSphere;    public Set<Integer> getRedSphere()    {        return redSphere;    }    public void setRedSphere ( Set<Integer> redSphere )    {        this.redSphere = redSphere;    }    public int getBlueSphere()    {        return blueSphere;    }    public void setBlueSphere ( int blueSphere )    {        this.blueSphere = blueSphere;    }    public String toString()    {        String sphereNumber = "";        Iterator<Integer> it = redSphere.iterator();        for ( Integer i : redSphere ) {            sphereNumber = sphereNumber + "\t" + i + "\t";        }        sphereNumber = sphereNumber + "\t" + blueSphere;        return sphereNumber;    }}

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

0 0
原创粉丝点击