-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole1.java
More file actions
76 lines (60 loc) · 2.28 KB
/
console1.java
File metadata and controls
76 lines (60 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.io.BufferedReader;
import java.io.Console;
import java.io.InputStreamReader;
import java.sql.Time;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.io.*;
public class console1 {
public String getStringRandom(int length) {
String val = "";
Random random = new Random();
//参数length,表示生成几位随机数
for(int i = 0; i < 6; i++) {
String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
//输出字母还是数字
if( "char".equalsIgnoreCase(charOrNum) ) {
//输出是大写字母还是小写字母
int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
val += (char)(random.nextInt(26) + temp);
} else if( "num".equalsIgnoreCase(charOrNum) ) {
val += String.valueOf(random.nextInt(10));
}
}
return val;
}
//生成随机数字和字母,
public static void main(String[] args) throws IOException{
String ran;
Object wat1 = new Object();
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
Console cons = System.console();
String username = cons.readLine("User name:");
char[] passwd = cons.readPassword("Passwd:");
int linelength = passwd.length;
console1 con1 = new console1();
String ran1 = con1.getStringRandom(6);
StringBuffer str = new StringBuffer(linelength);
Thread t1 = new Thread();
try {
for (int j = 0; j < linelength; j++) {
str.append("*");
}
System.out.println("用户名为:" + username);
System.out.println("密码为:" + str);
System.out.println("验证码:" + ran1);
System.out.println("输入验证码显示密码");
ran = buf.readLine();
if(ran.equals(ran1)){
System.out.println("用户名为:" + username);
System.out.println("密码为:" + String.valueOf(passwd));
}else{
System.out.println("错误,按三秒后退出");
TimeUnit.SECONDS.sleep(3);
}
}catch (Exception e) {
e.printStackTrace();
}
t1.start();
}
}