JAVA输出一组双色球

难点:红球不能一样~

实现方法:使用util库的Random获取随机数,再拼装为数组

package org.jalena;

import java.util.Random;

public class OutNumber {

	public OutNumber() {
	}

	private int[] getRedBall() {

		Random num = new Random(); // new一个随机数出来
		int[] redBall = new int[6]; // 声明一个6个数的int数组

		boolean result = false;

		do {
			int count = 0;
			result = false; // 循环赋值

			// 生成一个数组
			for (int i = 0; i < redBall.length; i++) {
				redBall[i] = num.nextInt(31) + 1;
			}

			// 判断数组是否有相同的
			for (int i = 0; i < redBall.length - 1; i++) {
				for (int j = i + 1; j < redBall.length; j++) {
					if (redBall[i] == redBall[j])
						count++;
				}
			}

			if (count > 0) {
				result = true;
			}

		} while (result);

		return redBall;
	}

	private String getBlueBall() {
		Random num = new Random();
		int blueBall = num.nextInt(31) + 1;

		String oblueBall;

		if (blueBall < 10) {
			oblueBall = "0" + blueBall;
		} else {
			oblueBall = "" + blueBall;
		}

		return oblueBall;
	}

	// 输出一组双色球号码
	public void getSsq() {

		// redBall
		System.out.print("红球是:");
		for (int a : this.getRedBall()) {
			if (a < 10) {
				System.out.print("0" + a + " ");
			} else {
				System.out.print(a + " ");
			}
		}

		// blueBall
		System.out.println("\t" + "蓝色球是:" + this.getBlueBall());

	}
}

There are no comment yet.

HTML tag cannot be used in this comment.