2016/1/14 作业 第一题 生成四位验证码 第二题 彩票机

 1 import java.util.Random;
 2 
 3 
 4 public class Test1 {
 5 
 6     /**
 7      * @param args
 8      */
 9     public static void main(String[] args) {
10         // TODO 自动生成的方法存根
11         
12         // 方法一: 全部列出      
13         String ss=new String("qwertyuiopasdfghjklzxcvbnm1234567890");
14         
15         //随机数实例化
16         Random hh=new Random();
17         
18         //产生字符串ss 字符长度-1的随机数   向前赋值
19         int k=hh.nextInt(ss.length()-1);
20         int l=hh.nextInt(ss.length()-1);
21         int m=hh.nextInt(ss.length()-1);
22         int n=hh.nextInt(ss.length()-1);
23         
24         //截取 赋值数与赋值数加一的下标  之间的字符
25         String a=ss.substring(k, k+1);
26         String b=ss.substring(l, l+1);
27         String d=ss.substring(m, m+1);
28         String c=ss.substring(n, n+1);
29         
30         //四次截取  分别输出  去换行
31         String e=a+b+d+c;
32         System.out.print("生成验证码 "+e);
33         System.out.println();
34         
35         //方法二  简化   for循环
36         String s=new String("qwertyuiopasdfghjklzxcvbnm1234567890");
37         Random ran = new Random();    //实例化
38         System.out.print("生成验证码 ");
39         for(int j=0;j<4;j++){
40         //产生的随机数与实例化后的赋值有关ran. 最大值受字符串length控制需减一  
41         //.nextInt();是对ran  采取的方法    
42             int r = ran.nextInt(s.length()-1);  
43             System.out.print(s.substring(r,r+1));    
44             }
45         
46 
47         }
48     }
49       

 搜到

 1 package caipiao;
 2 
 3 import java.util.Random;
 4 
 5 public class CaiPiao {
 6 
 7     public static void main(String[ ] args) {
 8         // 36选7
 9         int[ ] caiPiao = new int[7];
10         Random rand = new Random();
11 
12         // 生成7个随机数。i代表正在生成第几个数
13         for (int i = 0; i < 7; i++) 
14         {  
15         // 生成随机数
16             int temp = rand.nextInt(36);
17             
18        // 解决重复问题
19             int chongFuCiShu = 0;         // 检索数组,记录重复次数
20             for (int j = 0; j < 7; j++) { // 检查是否有重复,有重复就记录chongFuCiShu++;
21                 if (temp == caiPiao[j]) {
22                     chongFuCiShu++;
23                 }
24             }
25 
26             if (chongFuCiShu == 0) {
27                 caiPiao[i] = temp;
28             } 
29             else {
30                 i--;
31             }
32 
33         }
34         // 显示
35         for (int i = 0; i < caiPiao.length; i++) {
36             System.out.print(caiPiao[i] + "\t");
37         }
38 
39     }
40 }

posted on 2016-01-15 01:14  一棵树2016  阅读(317)  评论(0编辑  收藏  举报