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 + ") "); } }
时间: 2024-10-04 07:19:50