包含6位数验证码java代码的词条

java中如何编写输出一组由大写和数字组成的6个随机验证码,字母O和I不能输出import java.util.Random;
public class Test {
public static void main(String[] args) {
char c = 'i';
StringBuffer sb = new StringBuffer();
for (int i = 0; i6; i) {
int a = Math.abs((new Random()).nextInt(57));// 产生0~57的随机数
if (a = 9) {// 将0~9转为char的0~9
sb.append((char) (a48));
} else if (a33) {// 将10~33转为char的A~Z
if((a55) == 79 || (a55) == 73){
sb.append((char) (a63));
}else{
sb.append((char) (a55));
}
} else {// 将33~57转为char的a~z
sb.append((char) (a63));
}
}
System.out.println("随机生成的6位密码为: "sb.toString());
}
}
JAVA程序中,随机产生一个6位的验证码 (是由0-9a-ZA-Z)组成 。请高手帮助,谢谢public String get(){String str="";for (int i=0;i6;i){int itmp = random.nextInt(26)65;char ctmp = (char)itmp;str =ctmp;}return str;}
用java生成6位验证码,由大小写数字组成,不能重复字符生成随机可以用Random类 , 不能重复可以考虑放在HashSet中,因为HashSet的值是不能重复的,看代码如下:
public class GenerateRandomLetterAndNum {
public static void main(String[] args) {
SetString store = getletterandnum(6);
printSet(store);
}
public static SetString getletterandnum(int length) {
SetString set =new HashSetString();
for (int i = 0; ilength; i) {
String value = https://www.04ip.com/post/getrandom();
set.add(value);
}
if (set.size()length) { //如果没有生成6位
String value = https://www.04ip.com/post/getrandom();//继续调用生成随机数的方法
set.add(value);
}
returnset;
}
private static String getrandom() { //生成随机字母和数字方法
String valuehttps://www.04ip.com/post/= "";
Random random = new Random();
int gen = random.nextInt(2);//0、1、2
String charornum = gen % 2 == 0 ? "char" : "num";
if ("char".equals(charornum)) {
int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
int ascii = random.nextInt(26);
value= https://www.04ip.com/post/(char) (asciitemp);
} else if ("num".equalsIgnoreCase(charornum)) {
value= https://www.04ip.com/post/String.valueOf(random.nextInt(10));
}
return value;
}
public staticvoid printSet(Set set){ //打印set的方法
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
String ele = (String) iterator.next();
System.out.print(ele " ");
}
}
用java生成6位验证码,要求字母大小写数字组成,不能重复字符public class GenerateRandomLetterAndNum {
public static void main(String[] args) {
SetString store = getletterandnum(6);
printSet(store);
}
public static SetString getletterandnum(int length) {
SetString set =new HashSetString();
for (int i = 0; ilength; i) {
String value = https://www.04ip.com/post/getrandom();
set.add(value);
}
if (set.size()length) { //如果没有生成6位
String value = https://www.04ip.com/post/getrandom();//继续调用生成随机数6位数验证码java代码的方法
set.add(value);
}
returnset;
}
private static String getrandom() { //生成随机字母和数字方法
String valuehttps://www.04ip.com/post/= "";
Random random = new Random();
int gen = random.nextInt(2);//0、1、2
String charornum = gen % 2 == 0 ? "char" : "num";
if ("char".equals(charornum)) {
int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
int ascii = random.nextInt(26);
value= https://www.04ip.com/post/(char) (asciitemp);
} else if ("num".equalsIgnoreCase(charornum)) {
value= https://www.04ip.com/post/String.valueOf(random.nextInt(10));
}
return value;
}
public staticvoid printSet(Set set){ //打印set6位数验证码java代码的方法
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
String ele = (String) iterator.next();
System.out.print(ele " ");
}
}
JAVA程序中,随机产生一个6位的验证码 (是由0import java.util.Random;
public class TestRandom {
public String getRandStr(int charCount) {
String charValuehttps://www.04ip.com/post/= "";
for (int i = 0; icharCount; i){
char c = (char) (randomInt(0,26) 'a');
charValue= https://www.04ip.com/post/String.valueOf(c);
}
return charValue;
}
public String getRandNum(int charCount) {
String charValuehttps://www.04ip.com/post/= "";
for (int i = 0; icharCount; i){
char c = (char) (randomInt(0,10) '0');
charValue= https://www.04ip.com/post/String.valueOf(c);
}
return charValue;
}
public int randomInt(int from, int to){
Random r = new Random();
return fromr.nextInt(to - from);
}
}
【包含6位数验证码java代码的词条】6位数验证码java代码的介绍就聊到这里吧 , 感谢你花时间阅读本站内容 , 更多关于、6位数验证码java代码的信息别忘了在本站进行查找喔 。

    推荐阅读