Java程序实现验证码

一、项目名称:Java程序实现验证码

二、源代码

import javax.swing.JOptionPane;

  

public class YanZhengMa

{

    public void createRandomStr(int weishu)

    {

     String str = "";

     for(int i=1;i<=weishu;i++)

     {

     //生成一个表示a~z的ASCII的随机数

            int intValue = (int)(Math.random()*26+97);

            //将此随机数转化为其对应的字母并连接在str后面

            str = str + (char)intValue;

            }

     }

    public static void main(String[] args)

    {

     //生成一个6位的由小写字母组成的随机字符串    

        int weishu=6;

        String str = "";

        {

         //生成一个表示a~z的ASCII的随机数

         int intValue = (int)(Math.random()*26+97);

             //将此随机数转化为其对应的字母并连接在str后面

             str = str + (char)intValue;

             }

        //随机字符串生成完毕

        String inputStr;

        inputStr = JOptionPane.showInputDialog("请输入以下验证码\n"+str);

        if(inputStr.equals(str))

         {

             JOptionPane.showMessageDialog(null, "验证成功");

         }

         else

        {

            JOptionPane.showMessageDialog(null, "抱歉,验证失败");

        }

    }

 }

三、截图

截图一

 

截图二

 

截图三

 

原文地址:https://www.cnblogs.com/dongge0611/p/4859014.html