所以最近我一直在编写这个密码检查器,我差不多完成了。对于我的密码检查器,有一些要求。
我已经成功编写了所有这些,现在我只编写了一个代码,列出了所有不能同时满足的所有要求,而不是一个一个。此外,当密码可以接受并且满足所有要求时。我感谢任何帮助,谢谢大家
import java.util.Scanner;
public class PasswordChecker {
public static void main(String[] args) {
Scanner james = new Scanner(System.in);
System.out.println("Hi there, Welcome to Password Heavean where we have the hardest password combinations on this easrth and any other earth.");
System.out.println("Take note your password must be longer than 8 and less than 20 characters, Can not have your first or last name, must use a @ sign, must have a number and use at leaast 3 capitals");
System.out.println("First please input you first name.");
String passWord;
String firstName;
String lastName;
String firstNameLower;
String lastNameLower;
String passWordLower;
firstName = james.nextLine();
firstNameLower = firstName.toLowerCase();
System.out.println("Also please input you Last name.");
lastName = james.nextLine();
lastNameLower = lastName.toLowerCase();
System.out.println("Please enter your password now");
passWord = james.nextLine();
passWordLower = passWord.toLowerCase();
if (passWordLower.length() < 9) {
while (passWordLower.length() > 20) {
System.out.println("Sorry but your password is greater than or equal too 20 characters, please try a differnent password");
passWord = james.nextLine();
}
}
if (passWordLower.indexOf(firstNameLower) != -1 || (passWordLower.indexOf(lastNameLower) != -1)) {
System.out.println("Sorry but you can not use your first or your last name in the password. ");
}
if (passWordLower.contains("@")) {} else {
System.out.println("Sorry but you must use a @ symbol in your password.");
}
if (passWordLower.contains("_")) {} else {
System.out.println("Sorry but you must use a underscore in your password.");
}
int counter = 0;
for (int i = 0; i < passWordLower.length(); i++) {
if (Character.isDigit(passWordLower.charAt(i))) {
counter++;
}
}
if (counter < 3) {
System.out.println("Sorry but you need to have at least 3 numbers in your password.");
}
}
}
答案 0 :(得分:0)
我也只用while块
更改了if + while块while (passWord.length() < 9 || passWord.length() > 20) {
System.out.println("Sorry but your password is greater than or equal too 20 characters, please try a differnent password");
passWord = james.nextLine();
}
passWordLower = passWord.toLowerCase();
String errorMessage = "";
if (passWordLower.indexOf(firstNameLower) != -1 || (passWordLower.indexOf(lastNameLower) != -1)) {
errorMessage += "Sorry but you can not use your first or your last name in the password. " + System.lineSeparator();
}
if (passWordLower.contains("@")) {} else {
errorMessage += "Sorry but you must use a @ symbol in your password." + System.lineSeparator();
}
if (passWordLower.contains("_")) {} else {
errorMessage += "Sorry but you must use a underscore in your password." + System.lineSeparator();
}
int counter = 0;
for (int i = 0; i < passWordLower.length(); i++) {
if (Character.isDigit(passWordLower.charAt(i))) {
counter++;
}
}
if (counter < 3) {
errorMessage += "Sorry but you need to have at least 3 numbers in your password." + System.lineSeparator();
}
System.out.println(errorMessage);
我认为你不会注意到它的区别,它打印得如此之快
答案 1 :(得分:0)
尝试这样的事情,
StringBuffer errorMgs = new StringBuffer();
errorMgs.append("Sorry !! ");
if (passWordLower.length() < 9) {
errorMgs.append("your password must have minimum 9 characters \nand");
}
else if (passWordLower.length() > 20) {
errorMgs.append("your password is greater than or equal too 20 characters \nand");
}
if (passWordLower.indexOf(firstNameLower) != -1 || (passWordLower.indexOf(lastNameLower) != -1)) {
errorMgs.append(" you can not use your first or your last name \nand");
}
if (!passWordLower.contains("@")) {
errorMgs.append(" you must use a @ symbol \nand");
}
if (!passWordLower.contains("_")) {
errorMgs.append(" you must use a underscore \nand");
}
int counter = 0;
for (int i = 0; i < passWordLower.length(); i++) {
if (Character.isDigit(passWordLower.charAt(i))) {
counter++;
}
}
if (counter < 3) {
errorMgs.append(" you need to have at least 3 numbers");
}
if (!errorMgs.toString().equals("Sorry !! ")) { //identify given password met all criterias or not
errorMgs.append(" in your password.");
System.out.println(errorMgs);
} else {
System.out.println("your password accepted");
}
你的结果将是这样的,
您好,欢迎来到我们最难的密码Heavean 这个easrth和任何其他地球上的密码组合。做记录 您的密码必须长于8且少于20个字符,可以 没有你的名字或姓氏,必须使用@符号,必须有 号码并在至少3个首都使用
首先请输入您的名字。
jhone
另请输入您的姓氏。
彼得
请立即输入您的密码
sld4f
抱歉!!你必须使用@符号,你必须使用下划线和 您需要在密码中至少包含3个号码。