码迷,mamicode.com
首页 > 编程语言 > 详细

java编写双色球源代码。-----系统作为彩票双色球生成器,模拟机选一注双色球的彩票号码

时间:2015-04-18 14:22:46      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

package demo2;
import java.util.Arrays;
import java.util.Random;
/**
 * 系统作为彩票双色球生成器,模拟机选一注双色球的彩票号码:
 * 	1、需要从“01”到“32”中随机选择出6个数字作为红色球且这6个数字不能重复;
 * 	2、并从”01”到”07”中随机选择一个数字作为蓝色球;
 * 	3、7个数字合到一起作为一注双色球彩票的号码;
 */
public class DoubleBall {
	public static void main(String[] args) {
		String[] RED_BALLS = { "01", "02", "03", "04", "05", "06", "07", "08",
				"09", "10", "11", "12", "13", "14", "15", "16", "17", "18",
				"19", "20", "21", "22", "23", "24", "25", "26", "27", "28",
				"29", "30", "31", "32" };
		String[] BLUE_BALLS = { "01", "02", "03", "04", "05", "06", "07" };
		boolean[] redFlags = new boolean[RED_BALLS.length];
		String[] redBalls = new String[6];
		String blueBall;
		Random ran = new Random();
		// red
		for (int i = 0; i < redBalls.length; i++) {
			int index;
			do {
				index = ran.nextInt(RED_BALLS.length);
			} while (redFlags[index]);
			/**
			 * redFlags[index]用途:
			 * 	当redFlags[index]=true表示已经重复,所以你需要
			 * 	再执行do当中的代码重新获取index
			 */
			redBalls[i] = RED_BALLS[index];
			redFlags[index] = true;
		}
		// blue
		blueBall = BLUE_BALLS[ran.nextInt(BLUE_BALLS.length)];
		Arrays.sort(redBalls);
		System.out.println("**********本期开奖**********");
		System.out.println("红球: ");
		for (int i = 0; i < redBalls.length; i++) {
			System.out.print("(" + redBalls[i] + ") ");
		}
		System.out.println();
		System.out.println("篮球: ");
		System.out.print("(" + blueBall + ") ");
	}
}

 

java编写双色球源代码。-----系统作为彩票双色球生成器,模拟机选一注双色球的彩票号码

标签:

原文地址:http://www.cnblogs.com/ouysq/p/4437269.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!