什么都不说了 ,直接上代码
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(); } }