java习题1 验证码

第一种

public static void main(String[] args) {
HashSet list=new HashSet();
HashSet list1=suiJi(list);
sop(list1);
}
public static HashSet suiJi(HashSet al) {

Random rd=new Random();
int m=0;
String n="";
String a="";
int b=0;
for (int i = 0; i < 4; i++) {
	String str="abcdefhijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXY";
	String str1="345678";
	m=rd.nextInt(46);
    n=""+str.charAt(m);
    b=rd.nextInt(6);
    a=""+str1.charAt(b);
    b=rd.nextInt(2);
    if(b==0) {
    	al.add(a);
    	
    }else {
    	al.add(n);
    }
}
return al;

}

第二种

public class ThreadWork {

//生成随机数字和字母,  
public String getStringRandom(int length) {  
      
    String val = "";  
    Random random = new Random();  
      
    //参数length,表示生成几位随机数  
    for(int i = 0; i < length; i++) {  
          
  String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";  
        //输出字母还是数字  
        if( "char".equalsIgnoreCase(charOrNum) ) {  
            //输出是大写字母还是小写字母  
            int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;  
            val += (char)(random.nextInt(26) + temp);  
        } else if( "num".equalsIgnoreCase(charOrNum) ) {  
            val += String.valueOf(random.nextInt(10));  
        }  
    }  
    return val;  
}  
  
public static void  main(String[] args) {  
    ThreadWork test = new ThreadWork();  
    //测试  
    System.out.println(test.getStringRandom(8));  
}  

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值