学习电脑 发表于 2017-7-13 14:56:10

用线程实现密码屏蔽

package caterpillar;
import java.util.*;

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 voidrun() {
                while(isActive()) {
                        System.out.print(mask);
                        try {
                                //暂停目前的线程50毫秒
                                Thread.currentThread().sleep(500);
                        }
                        catch(InterruptedException e) {
                                e.printStackTrace();
                        }
                }
        }
}

public class EraserThread {

        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);
                       
                        if("caterpillar".equals(name)&&"123456".equals(password)) {
                                System.out.println("欢迎caterpillar");
                                break;
                        }
                        else {
                                System.out.printf("%s,名称或密码错误,请 重新输入! %n", name);
                        }
                }

        }

}


为什么我在Eclipse中运行时,一直跳出#,而不会实现密码屏蔽?

学习电脑 发表于 2017-7-13 19:17:08

dinga

google_boy 发表于 2017-7-17 23:28:39

String password = input.next();
这一句读取到的数据中包含"\010"+maskChar"字符,所以要进行处理
String password = input.next().replace("\010"+maskChar", "");

学习电脑 发表于 2017-7-19 21:18:56

google_boy 发表于 2017-7-17 23:28
String password = input.next();
这一句读取到的数据中包含"\010"+maskChar"字符,所以要进行处理
Str ...

怎么改啊? 能发下代码吗? 你那条语句还是错误的

google_boy 发表于 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 voidrun() {
            while(isActive()) {
                  System.out.print(mask);
                  try {
                            //暂停目前的线程50毫秒
                            Thread.currentThread().sleep(500);
                  }
                  catch(InterruptedException e) {
                            e.printStackTrace();
                  }
            }
    }
}

google_boy 发表于 2017-7-20 09:23:04

"\010"的字符没显示出来
口述一下:
password = password.replace("替换的字符串", "");
这里替换的字符串就是你生成的干扰密码显示的("\010"+maskChar)在控制台输出的字符串

学习电脑 发表于 2017-7-21 09:22:05

google_boy 发表于 2017-7-20 09:23
"\010"的字符没显示出来
口述一下:
password = password.replace("替换的字符串", "");


我复制了你的代码,但是达不到那种效果啊,边输入边隐藏的。

学习电脑 发表于 2017-7-21 09:32:56

google_boy 发表于 2017-7-20 09:23
"\010"的字符没显示出来
口述一下:
password = password.replace("替换的字符串", "");


我输入的密码还是会显示出来

无聊才上线 发表于 2017-7-21 14:14:44

我觉得是while true的问题,所以一直替换吗,想输一个替换一个,需要用监听器

学习电脑 发表于 2017-7-21 15:36:27

无聊才上线 发表于 2017-7-21 14:14
我觉得是while true的问题,所以一直替换吗,想输一个替换一个,需要用监听器

怎么改你说下
页: [1]
查看完整版本: 用线程实现密码屏蔽