*随机6+1,开发实例

package cn.com.yitong.test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;

/*
* 红球 1~33 篮球1-16
*/
public class Demo {

private static Set<Integer> set = new HashSet<Integer>();
private static ArrayList<Integer> list = new ArrayList<Integer>();
//private static int[] arr = new int[6];
//private static int[] arrL = new int[7];
private static int time = 0;
private static Random random = null;
private static int redBallMax=33;
private static int blueBallMax=16;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入注数:");
int number = sc.nextInt();
int count = 0;
while (true) {
count++;
if (count > number) {
break;
}
getRedBall();
getBlueBall();
getArr();
}
}

/*
* 从1-33个数字中随机取6个红球
*/
public static void getRedBall() {
random = new Random();
int redNum = 0;
while (true) {
redNum = set.size();

if (redNum >= 6) {
break;
}
set.add(random.nextInt(redBallMax) + 1);
// System.out.print(red+1+" ");
}
Iterator<Integer> iterator = set.iterator();
// int s = 0;
while (iterator.hasNext()) {
int next = iterator.next();
list.add(next);

// arr[s] = next;
// s++;
}
// Arrays.sort(arr);
Collections.sort(list);
set.clear();
}

/*
* 从1-16中取1个篮球
*/
public static void getBlueBall() {
random = new Random();
int count = 0;
int blue = random.nextInt(blueBallMax) + 1;
// arrL[6] = blue;
list.add(blue);
}

// System.out.print(blue+1+" ");
/*
* 取出数组
*/
public static void getArr() {
time++;
// int j = 0;
// for (int i = 0; i < arr.length; i++) {
// arrL[j] = arr[i];
// j++;
// }
// /**
// * 遍历数组arrL
// */
if (time > 1) {
System.out.println();
}
// for (int i = 0; i < arrL.length; i++) {
// if (arrL[i] >= 10) {
// System.out.print(arrL[i] + " ");
// } else {
// System.out.print("0" + arrL[i] + " ");
// }
// }

/* Iterator<Integer> iterator = list.iterator();
while(true){
int next = iterator.next();
System.out.print(next+" ");
}*/

for(int i =0;i<list.size();i++){
int next = list.get(i);

if(next<10){
System.out.print("0"+next+" ");
}else{
System.out.print(next+" ");
}
}
list.clear();

}

}

运行结果:

请输入注数:10
01 19 27 28 30 32 04
04 05 16 24 25 28 10
04 06 09 10 13 17 11
01 02 10 13 21 24 05
03 11 13 19 32 33 11
05 07 10 18 24 27 05
01 06 07 21 23 29 10
09 10 17 21 22 24 11
01 05 20 24 27 28 09

原文地址:https://www.cnblogs.com/wpf-7/p/9646205.html