加个产生n组随机数据的函数
程序代码:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class Dr {
private int lastNum;
private Random random = new Random();
private List<Integer> list = new ArrayList<Integer>();
private List<Integer> numList = new ArrayList<Integer>();
public Dr(){
init();
}
private void init(){
list.clear();
numList.clear();
for(int i = 1; i<=33; i++){
list.add(i);
}
}
public void generateNum(){
for(int i =0; i<6; i++){
numList.add(list.remove(random.nextInt(list.size())));
}
}
public void generateLastNum(){
int index = getLastNumMaxIndex();
lastNum = list.remove(random.nextInt(index));
}
private int getLastNumMaxIndex(){
Collections.sort(list);
for(int i =0; i<list.size(); i++){
if(list.get(i)>16)
return i-1;
}
return -1;
}
public void printRandomNums(){
Collections.sort(numList);
for(int i = 0;i<6;i++){
System.out.printf("%3d", numList.get(i));
}
System.out.printf("%3d\n",lastNum);
}
public void generateRandomNums(int n){
for(int i =0; i<n; i++){
init();
generateNum();
generateLastNum();
printRandomNums();
}
}
public static void main(String[] args) {
Dr d = new Dr();
d.generateNum();
d.generateLastNum();
d.printRandomNums();
d.generateRandomNums(8);
}
}