利用随机函数nextInt实现*37选7

 1 package cn.demo;
 2 
 3 import java.util.Arrays;
 4 import java.util.Random;
 5 
 6 public class TestDemo {
 7     public static void main(String[] args) {
 8         int data[] = new int [7];
 9         int foot = 0;   //控制脚标
10         Random rand = new Random();
11         while(foot < data.length){  //数量不住
12             int num = rand.nextInt(37);  //生成一个随机数
13             if(isExists(data,num,foot)){  //可以使
14                 data[foot ++] = num;
15             }
16         }
17         System.out.print("中奖号码:");
18         Arrays.sort(data);
19         for(int x = 0;x<data.length;x++){
20             System.out.print(data[x] + "、");
21         }
22     }
23     public static boolean isExists(int temp[],int num,int foot){ //判断数字是否存在
24         if (num == 0){
25             return false;
26         }
27         for(int x = 0;x<foot;x++){
28             if(temp[x] == num){
29                 return false;
30             }
31         }
32         return true;
33     }
34 }

输出结果:

中奖号码:*、*、*、*、*、*、*、

原文地址:https://www.cnblogs.com/liyang31/p/5803010.html