验证码
package random;
import java.util.Random;
import java.util.Scanner;
public class RandomStr
{
public static void main(String[] args)
{
int k = 0;
while(k<1){//多次验证
Random n = new Random();//随机函数
//定义一个空字符串
String result = "";
//进行6次循环
for(int i = 0 ; i < 6 ; i ++)
{
int j = n.nextInt(2);//使验证码有数字也有字符
int intVal;
if( j == 0){
//生成一个97~122的int型的整数
intVal = (int)(Math.random() * 26 + 97);
//将intValue强制转换为char后连接到result后面
result = result + (char)intVal;
}
else {
intVal = n.nextInt(10);
result = result + (int)intVal;
}
}
//输出随机字符串
System.out.println("输入验证码:"+result);
Scanner scanner = new Scanner(System.in);
String p = scanner.next();
if( p.equals(result)==true ) //判断验证码是否正确
{
System.out.println("验证码正确!");
k = 1;
}
else System.out.println("验证码错误!");
}
}
}