java mask_java – 带字符的Mask字符串

这是用于输入密码吗?考虑以下:

class Password {

final String password; // the string to mask

Password(String password) { this.password = password; } // needs null protection

// allow this to be equal to any string

// reconsider this approach if adding it to a map or something?

public boolean equals(Object o) {

return password.equals(o);

}

// we don't need anything special that the string doesnt

public int hashCode() { return password.hashCode(); }

// send stars if anyone asks to see the string - consider sending just

// "******" instead of the length,that way you don't reveal the password's length

// which might be protected information

public String toString() {

StringBuilder sb = new StringBuilder();

for(int i = 0; < password.length(); i++)

sb.append("*");

return sb.toString();

}

}

或者对于刽子手的方法

class Hangman {

final String word;

final BitSet revealed;

public Hangman(String word) {

this.word = word;

this.revealed = new BitSet(word.length());

reveal(' ');

reveal('-');

}

public void reveal(char c) {

for(int i = 0; i < word.length; i++) {

if(word.charAt(i) == c) revealed.set(i);

}

}

public boolean solve(String guess) {

return word.equals(guess);

}

public String toString() {

StringBuilder sb = new StringBuilder(word.length());

for(int i = 0; i < word.length; i++) {

char c = revealed.isSet(i) ? word.charAt(i) : "*";

}

return sb.toString();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值