java 生成双色球彩票

什么都不说了 ,直接上代码

package com.java.color.ticket;

import java.util.Arrays;
import java.util.Random;

public class ColorTicket {

	/**
	 * 蓝球的可选范围
	 */
	private static final int[] BLUEBALL = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
			12, 13, 14, 15, 16 };
	/**
	 * 红球的可选范围
	 */
	private static final int[] REDBALL = { 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 int blueBall = 0;

	/**
	 * 保存6红球
	 */
	private int[] redBall = new int[6];

	/**
	 * 生成一组彩票,一个蓝球 6个红球
	 * 
	 * @return
	 */
	public String randomTicket() {
		randomBlueBall();
		randomRedBall();

		final StringBuffer buffer = new StringBuffer();
		buffer.append(blueBall + "\t" + "|" + "\t");
		for (int i = 0; i < redBall.length; i++) {
			if (i == redBall.length - 1)
				buffer.append(redBall[i]);
			else
				buffer.append(redBall[i] + "\t");
		}
		return buffer.toString();
	}

	/**
	 * 随机生机蓝球一个
	 */
	private void randomBlueBall() {
		blueBall = BLUEBALL[randomInt(BLUEBALL.length)];
	}

	private void randomRedBall() {
		int index = 0;
		int num = 0;
		boolean con = false;
		while (index < redBall.length) {
			num = REDBALL[randomInt(REDBALL.length)];
			if (blueBall > 0 && num != blueBall) {
				con = isInNum(redBall, num);
				if (con) {
					redBall[index] = num;
					index++;
				}
			}
		}
		Arrays.sort(redBall);
	}

	/**
	 * 在一定范围内生成一个随机数
	 * 
	 * @param range
	 * @return
	 */
	private int randomInt(int range) {
		int index = 0;
		do {
			final Random random = new Random();
			index = random.nextInt(range);
		} while (index < 0);
		return index;
	}

	/**
	 * 判断指定的数组里是否包含指定的数字
	 * 
	 * @param array
	 * @param num
	 * @return
	 */
	private static boolean isInNum(int[] array, int num) {
		for (int n : array) {
			if (num == n)
				return false;
		}
		return true;
	}
} 

  


调用

 

package com.java.color.ticket;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
	public static void main(String[] args) throws IOException {
		final StringBuffer buffer = new StringBuffer();
		final ColorTicket colorTicket = new ColorTicket();
		int i = 0;
		final int max = 5;
		for (; i < max; i++) {
			final String str = colorTicket.randomTicket();
			buffer.append(str);
			buffer.append("\r\n");
		}
		final String path = System.getProperty("user.dir");
		final FileOutputStream out = new FileOutputStream(path + File.separator
				+ "colorticket.txt");
		out.write(buffer.toString().getBytes());
		out.flush();
		out.close();
	}
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值