每日随笔7

验证码程序  其中加入了图形界面  需要用到JOptionPane包

package vec;

import java.util.Random;

import javax.swing.JOptionPane;

public class code {
// 返回一个大写字母
public char getCapital() {
Random rand = new Random();
int temp = rand.nextInt(90 - 65 + 1) + 65;
char temp1 = (char) temp;
return temp1;

}

// 返回一个小写字母
public char getLowercase() {
Random rand = new Random();
int temp = rand.nextInt(122 - 97 + 1) + 97;
char temp1 = (char) temp;
return temp1;
}

// 返回一个数字
public int getFigure() {
Random rand = new Random();
int temp = rand.nextInt(9 - 0 + 1);
return temp;
}

// 返回整个验证码
public String allvefi() {
Random rand = new Random();
int temp;
String s = new String();
for (int i = 0; i < 6; i++) {
temp = rand.nextInt(3 - 1 + 1) + 1;
switch (temp) {
case 1:
s = s + getCapital();
break;
case 2:
s = s + getLowercase();
break;
case 3:
s = s + getFigure();
break;
}
}
return s;
}

// 判断对错
public boolean judge(String temp1, String temp2) {
if (temp1.equals(temp2)) {
return true;
} else {
return false;
}
}

// 对错窗口
public void windows(boolean t) {
if (t) {
JOptionPane.showMessageDialog(null, "输入正确");
} else {
JOptionPane.showMessageDialog(null, "输入错误");
}
}

public static void main(String[] args) {
code temp = new code();
boolean a = true;
while (a) {
String b = temp.allvefi();
String s = JOptionPane.showInputDialog("验证码:" + b);
boolean t = temp.judge(b, s);
temp.windows(t);
if (t) {
a = false;
} else {
JOptionPane.showMessageDialog(null, "请再次输入");
}
}
}
}

posted @ 2020-10-02 20:04  不详·Christina  阅读(51)  评论(0)    收藏  举报