随机验证码。

随机验证码。

随机生成十组六位字符组成的验证码。

验证码由大小写字母、数字字符组成。

package com.sggu.coll.demo07;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.Random;



public class Test01 {

@SuppressWarnings("all")

public static void main(String[] args) {

char[]arr = new char[26+26+10];

//使用字符串数组保存原始字符

for (int i = 0; i <arr.length ; i++) {

if (i<10){//前十个放数字

arr[i]=(char)(i+48);//0-9就是48到57

}else if (i<10+26){//中间26个放大写字母

arr[i]=(char)(i+65-10);

}else {//剩下的放小写字母

arr[i]=(char)(i+97-26-10);

}

}

//随机生成10组验证码

ArrayList list=new ArrayList();

Random rand =new Random();

for (int i = 0; i <10 ; i++) {

String str ="";

for (int j = 0; j <6 ; j++) {

int index =rand.nextInt(arr.length);

str+=arr[index];//

}

list.add(str);

}

Iterator iter =list.iterator();

while (iter.hasNext()){

System.out.println("随机验证码:"+iter.next());

}

}

}

Guess you like

Origin blog.csdn.net/smwdgdg/article/details/121429396