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

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 + ") ");
	}
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。