程序设计思想:
百度相关函数知识及相关代码并借鉴,编写函数随机输出6个字符,并在主函数中调用这个函数,并和用户输入的函数进行比较。
程序流程图:
源程序:
import java.util.Random;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Verification {
public static String getRandomString(int length) {
String str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
Random random = new Random();
StringBuffer x = new StringBuffer();
for (int i = 0; i < length; ++i) {
int number = random.nextInt(62);
x.append(str.charAt(number));
}
return x.toString();
}
public static void main(String[] args) {
JFrame frame=new JFrame();
frame.setTitle("随机验证界面");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
String inviteCode = "";
for (int i = 0; i < 10000000; i++) {
inviteCode = getRandomString(6);
}
JLabel label = new JLabel("随机验证码:"+inviteCode);
JLabel label1 = new JLabel("请输入验证码:");
frame.getContentPane().add(label);
JLabel userLabel = new JLabel("User:");
JPanel panel = new JPanel();
userLabel.setBounds(10,20,80,25);
panel.add(userLabel);
JTextField userText = new JTextField(20);
userText.setBounds(100,20,165,25);
panel.add(userText);
frame.setVisible(true);
Scanner in=new Scanner(System.in);
String a;
a=in.next();
if(a.equals(inviteCode)==true)
System.out.println("输入正确!");
else
System.out.println("输入错误!");
}
}