美文网首页
从1-33个数中随机6个不同的数字,模拟双色球

从1-33个数中随机6个不同的数字,模拟双色球

作者: 被风扬起的沙 | 来源:发表于2018-05-21 20:00 被阅读35次
public class RandomNum6 {
    public static void main(String[] args) {
        int arrRed[] = new int[6];//用来存放红球
        int count = 0;
        boolean flag=true;
        while (count<6){
            flag=true;
            //随机一个数1-33
            int random=1+(int)(Math.random()*33);
            //判断 random 是否存在指定的数组中
            for (int i = 0; i <arrRed.length ; i++) {
                if (random==arrRed[i]){
                    flag=false;
                    break;
                }
            }
            if (flag){
                arrRed[count++]=random;
            }
        }
        //打印输出
        for (int i :arrRed){
            System.out.print(i+"\t");
        }
    }
}

此处要特别注意 Math.random()的取值范围是[0,1)

相关文章

网友评论

      本文标题:从1-33个数中随机6个不同的数字,模拟双色球

      本文链接:https://www.haomeiwen.com/subject/bzbqjftx.html