标签:
package org.tarena.day05; import java.util.Arrays; import java.util.Random; public class Double { public static void main(String[] args) { // TODO Auto-generated method stub String 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","33"}; //定义一个boolean类型数组b,其中全部的balls.length个元素都默认是false boolean[] b = new boolean[balls.length]; String [] redBalls = new String[6]; Random r = new Random(); int i = 0; while(true){ //nextInt(n)它是生成一个随机的int值,范围是从0(包括)和n(不包括)之间。 int index = r.nextInt(balls.length); if(b[index]){ continue; } redBalls[i++] = balls[index]; b[index] = true; //检查redballs数组是否存满, if(i == redBalls.length) break; } //红球数组排序 Arrays.sort(redBalls); //红球数组扩容后形成新数组 String[] allBalls=Arrays.copyOf(redBalls,redBalls.length+1); //随机抽取"0"-"16"号码的球放在新数组的最后一个位置 allBalls[allBalls.length-1]=balls[r.nextInt(16)]; System.out.println(Arrays.toString(allBalls)); }
标签:
原文地址:http://www.cnblogs.com/liaoliao/p/5025139.html