日常Java 2021/9/23
作者:互联网
练习使用Math.random函数,以及JOptionPane.showMessageDialog(null,"字符串","Results",JOptionPane.PLAIN_MESSAGE );实现自动生成验证码
package m;
import java.awt.*;
import javax.swing.*;
public class RandomStr {
public static void main(String[] args) {
String str = "";//定义一个空字符串
int flag=0;
do {
for(int i = 0 ; i < 6 ; i ++)//进行6次循环
{
int integer = (int)(Math.random() * 26 + 97);//生成一个97~122的int型的整数
str = str + (char)integer;//将integer强制转换为char后连接到str后面
}
String input=JOptionPane.showInputDialog(str+"\n请输入验证码:");//显示验证码,并提示用户输入验证码
if(input.equals(str))//判断验证码是否正确
{
JOptionPane.showMessageDialog(null,"验证码正确,验证成功!","Results",JOptionPane.PLAIN_MESSAGE );
flag=1;
}else{
JOptionPane.showMessageDialog(null,"验证码错误,请再次验证","Results",JOptionPane.PLAIN_MESSAGE );
}
}while(flag==0);
}
}
标签:Java,23,int,JOptionPane,验证码,Results,showMessageDialog,2021,str 来源: https://www.cnblogs.com/dzwj/p/15327992.html