实验四验证码

import java.util.Scanner;
public class RandomStr
{
public static void main(String[] args)
{
//定义一个空字符串
String result = "";
//进行6次循环
for(int i = 0 ; i < 6 ; i ++)
{
//生成一个97~122的int型的整数
int intVal = (int)(Math.random() * 26 + 97);
//将intValue强制转换为char后连接到result后面
result = result + (char)intVal;
}
//输出随机字符串
System.out.println(result);
Scanner sc = new Scanner(System.in);
System.out.println("请输入验证码");
String str =sc.nextLine();
if(str.equals(result)){
System.out.println("输入正确!");
}
else{
System.out.println("输入错误!");
}
}
}

原文地址:https://www.cnblogs.com/lwq666/p/7634320.html