前言
通过输入摇号次数,摇出每次前区和后区的数据,前区6个1~33的数据,后区1个1~16的数据,每个区内的所有数据不重复
源代码
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| package io.github.feiju12138;
import java.util.Random; import java.util.Scanner;
public class Lottery {
public static int[] before = new int[6]; public static int after = 0;
public static void start() {
Random r = new Random();
int index = 0;
while(true) {
int num = r.nextInt(33)+1;
if(!check(num)) { before[index] = num; index++; }
if(index==6) { break; }
}
after = r.nextInt(16)+1;
System.out.println("本次双色球摇号为:"); System.out.print("前区"); for(int i = 0; i < before.length; i++) { System.out.print(" "+before[i]); } System.out.println(); System.out.println("后区 "+after);
}
public static boolean check(int num) {
boolean key = false;
for(int i = 0; i < before.length; i++) {
if(num==before[i]) { key = true; }
}
return key;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); System.out.print("请输入摇号次数:"); int num = sc.nextInt();
Lottery lottery = new Lottery(); for(int i = 0; i < num; i++) { lottery.start(); }
}
}
|
完成