アビー:
ユーザーにパスワードの入力が1つだけの数字が含まれている場合、私は見つける必要があるだろうと私は、if文がどうなるか問題を抱えています。
私は現在、唯一持っています:
import java.util.Scanner;
public static boolean onlyOneDigit (String password) {
if ( ........ ) {
System.out.print("Password requirement not met");
return true;
} else {
return false;
}
}
public static void main(String[] args) {
do {
System.out.print("Enter a password");
String password = input.next();
} while (onlyOneDigit(password));
{
System.out.print("Password updated");
}
}
WJS:
これを試して。ノー桁またはパスワードで複数の対パスワードで一桁。
for (String pw : new String[]{"with1digit", "withtwo22digits","withNodigits",
"with2scatt2reddi3gits"}) {
boolean m = pw.matches("[^\\d]*\\d[^\\d]*");
System.out.println(m + " : " + pw);
}
次のように出力されます
true : with1digit
false : withtwo22digits
false : withNodigits
false : with2scatt2reddi3gits
使用例。
if (onlyOneDigit(password)) {
// it's good
} else {
// warn user
}
public static boolean onlyOneDigit(String pw) {
return pw.matches("[^\\d]*\\d[^\\d]*");
}
そして、彼女は別の方法です
public static boolean onlyOneDigit(String pw) {
// get rid of the first digit
String save =pw.replaceFirst("\\d","");
// replace the next digit with a #
save = save.replaceFirst("\\d", "#");
// if save contains # it had more than 1 digit
// or if save equals the original password
// then there were no digits.
return !(save.equals(pw) || save.contains("#"));
}