|
发表于 2017-7-20 09:18:14
|
显示全部楼层
package test;
import java.util.Scanner;
public class One {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while(true) {
System.out.print("输入名称:");
String name = input.next();
System.out.print("输入密码:");
//启动Eraser线程
EraserThreads eraser = new EraserThreads('#');
eraser.start();
String password = input.next();
eraser.setActive(false);
//因为"\010"+maskChar在控制台输出是"#",所以直接将这两个字符替换为空""
password = password.replace("#", "");
if("caterpillar".equals(name)&&"123456".equals(password)) {
System.out.println("欢迎caterpillar");
break;
}
else {
System.out.printf("%s,名称或密码错误,请 重新输入! %n", name);
}
}
}
}
class EraserThreads extends Thread{
private boolean active;
private String mask;
public EraserThreads() {
this('*');
}
public EraserThreads(char maskChar) {
active = true;
mask = "\010"+maskChar;
}
//停止线程时设置为false
public void setActive(boolean active) {
this.active = active;
}
public boolean isActive() {
return active;
}
public void run() {
while(isActive()) {
System.out.print(mask);
try {
//暂停目前的线程50毫秒
Thread.currentThread().sleep(500);
}
catch(InterruptedException e) {
e.printStackTrace();
}
}
}
} |
|