用循环和数组实现双色球
用for循环写 双色球的随机数实现(6个蓝球+1个红球),
要求:生成的6个红球号码不重复,红色取0-33,蓝球大小0-16
1 import java.util.Arrays; 2 import java.util.Random; 3 4 public class TwoClass6 { 5 public static void main(String[] args) { 6 int[] redBallS; 7 redBallS= new int[33];//红球数组 int[] redBalls = new int(33); 8 boolean[] redBallFlag = new boolean[33]; 9 Random redSN = new Random();//随机 10 for(int i=0;i<33;i++){ 11 redBallS[i] = i+1;//红球数组中放符合的数字 12 } 13 int[] choosenRedBall ;//红球的中奖6个 int[] choosenRedBall=new ine[6]; 14 choosenRedBall= new int[6]; 15 //int length = 0;//判断循环几次的值 16 for( int length = 0;length != 6;length++){//开始 17 int index = redSN.nextInt(33); 18 if(!redBallFlag[index]){//判断 19 redBallFlag[index] = true;//选中红球 去重 20 choosenRedBall[length] = redBallS[index];//将随机选中的数字放入数组 21 22 } 23 } 24 int blueBall = redSN.nextInt(17);//蓝色球0-16 25 //打印输出结果 26 System.out.println("红色球:"+Arrays.toString(choosenRedBall)+",蓝色球"+blueBall); 27 } 28 }

浙公网安备 33010602011771号