我的程序似乎遇到了一些问题。我被建议我的讲师使用do while循环来保持程序运行。但是,当我输入无效密码并重新启动时,它会出错。当我输入有效密码时,它只打印“有效密码”并且不循环。我哪里出错?
import java.util.Scanner;
public class CovenduniteProg5_FIX {
public static void main(String[] args) {
boolean passwordValid = false;
do {
boolean invalidLength = false;
boolean containsRestrictedWord = false;
boolean containsLowerCaseLetter = false;
boolean containsUpperCaseLetter = false;
boolean containsDigit = false;
boolean containsSpecialChar = false;
Scanner stdIn = new Scanner(System.in);
System.out.println("Password Verifier");
System.out.println("\nEnter a password that meets the following rules:\n" +
"\tIs at least 8 characters long\n" +
"\tContains at least 1 lower letter character\n" +
"\tContains at least 1 upper letter character\n" +
"\tContains at least 1 numberic digit\n" +
"\tContains at least 1 special character from the set: !@#$%^&*\n" +
"\tDoes not contain the word \"and\" or the word \"the\"\n");
System.out.print("Enter your password: ");
String password = stdIn.nextLine();
for (int i = 0; i < password.length(); i++) {
char ch = password.charAt(i);
if (Character.isUpperCase(ch) && !containsUpperCaseLetter) {
containsUpperCaseLetter = true;
}
if (Character.isLowerCase(ch) && !containsLowerCaseLetter) {
containsLowerCaseLetter = true;
}
if (Character.isDigit(ch) && !containsDigit) {
containsDigit = true;
}
if ((ch == '!' || ch == '@' || ch == '#' || ch == '$' || ch == '%' || ch == '^' || ch == '&' || ch == '*') && !containsSpecialChar) {
containsSpecialChar = true;
}
}
if (password.length() < 8 ) {
invalidLength = true;
}
if (password.contains("and") || password.contains("the")) {
containsRestrictedWord = true;
}
if (invalidLength) {
System.out.println("Invalid: Invalid length");
}
if (containsRestrictedWord) {
System.out.println("Invalid: Contains either the word \"and\" or \"the\".");
}
if (!containsDigit) {
System.out.println("Invalid: Does not contain at least one digit.");
}
if (!containsLowerCaseLetter) {
System.out.println("Invalid: Does not contain at least one lowercase letter.");
}
if (!containsUpperCaseLetter) {
System.out.println("Invalid: Does not contain at least one uppercase letter.");
}
if(!containsSpecialChar) {
System.out.println("Invalid: Does not contain at least one special character.");
}
if (containsSpecialChar && containsUpperCaseLetter && containsLowerCaseLetter && containsDigit && !containsRestrictedWord && !invalidLength)
passwordValid = true;
if (passwordValid)
System.out.print("\nPassword is valid.");
} while (!passwordValid);
passwordValid = false;
while(!passwordValid) {
if (passwordValid)
System.out.print("\nPassword is valid.");
}
}
}
答案 0 :(得分:0)
您将最后一个passwordValid = false;
放在do-while
之外,当您在循环之前只需要它一次。而且您也不需要第二个while(!passwordValid)
循环:
boolean passwordValid = false; //← only here
do {
boolean invalidLength = false;
boolean containsRestrictedWord = false;
boolean containsLowerCaseLetter = false;
boolean containsUpperCaseLetter = false;
boolean containsDigit = false;
boolean containsSpecialChar = false;
Scanner stdIn = new Scanner(System.in);
/*
...
*/
if (/*All sub-conditions met*/)
passwordValid = true;
if (passwordValid)
System.out.print("\nPassword is valid.");
}
while (!passwordValid);
//passwordValid = false; ← remove this
//no need for while or if statment, because you can only reach this code if passwordValid = true
System.out.print("\nPassword is valid.");
答案 1 :(得分:0)
我们不需要第二个while
循环,它基本上是infinite loop
。此外,我们需要在while
循环之外移动“密码验证程序”语句,因为它们不需要多次打印。所以,该方法看起来像这样:
public static void main(String[] args) {
boolean passwordValid = false;
System.out.println("Password Verifier");
System.out.println("\nEnter a password that meets the following rules:\n" + "\tIs at least 8 characters long\n"
+ "\tContains at least 1 lower letter character\n" + "\tContains at least 1 upper letter character\n"
+ "\tContains at least 1 numberic digit\n"
+ "\tContains at least 1 special character from the set: !@#$%^&*\n"
+ "\tDoes not contain the word \"and\" or the word \"the\"\n");
do {
boolean invalidLength = false;
boolean containsRestrictedWord = false;
boolean containsLowerCaseLetter = false;
boolean containsUpperCaseLetter = false;
boolean containsDigit = false;
boolean containsSpecialChar = false;
Scanner stdIn = new Scanner(System.in);
System.out.print("Enter your password: ");
String password = stdIn.nextLine();
for (int i = 0; i < password.length(); i++) {
char ch = password.charAt(i);
if (Character.isUpperCase(ch) && !containsUpperCaseLetter) {
containsUpperCaseLetter = true;
}
if (Character.isLowerCase(ch) && !containsLowerCaseLetter) {
containsLowerCaseLetter = true;
}
if (Character.isDigit(ch) && !containsDigit) {
containsDigit = true;
}
if ((ch == '!' || ch == '@' || ch == '#' || ch == '$' || ch == '%' || ch == '^' || ch == '&'
|| ch == '*') && !containsSpecialChar) {
containsSpecialChar = true;
}
}
if (password.length() < 8) {
invalidLength = true;
}
if (password.contains("and") || password.contains("the")) {
containsRestrictedWord = true;
}
if (invalidLength) {
System.out.println("Invalid: Invalid length");
}
if (containsRestrictedWord) {
System.out.println("Invalid: Contains either the word \"and\" or \"the\".");
}
if (!containsDigit) {
System.out.println("Invalid: Does not contain at least one digit.");
}
if (!containsLowerCaseLetter) {
System.out.println("Invalid: Does not contain at least one lowercase letter.");
}
if (!containsUpperCaseLetter) {
System.out.println("Invalid: Does not contain at least one uppercase letter.");
}
if (!containsSpecialChar) {
System.out.println("Invalid: Does not contain at least one special character.");
}
if (containsSpecialChar && containsUpperCaseLetter && containsLowerCaseLetter && containsDigit
&& !containsRestrictedWord && !invalidLength)
passwordValid = true;
if (passwordValid)
System.out.print("\nPassword is valid.");
} while (!passwordValid);
}
答案 2 :(得分:0)
以下代码将按您的要求执行。有些事情需要考虑:
希望这有帮助
// CovenduniteProg5_FIX
public static void main(String[] args) {
boolean passwordValid = false;
Scanner stdIn = new Scanner(System.in);
do {
System.out.println("Password Verifier");
System.out.println("\nEnter a password that meets the following rules:\n"
+ "\tIs at least 8 characters long\n" + "\tContains at least 1 lower letter character\n"
+ "\tContains at least 1 upper letter character\n" + "\tContains at least 1 numberic digit\n"
+ "\tContains at least 1 special character from the set: !@#$%^&*\n"
+ "\tDoes not contain the word \"and\" or the word \"the\"\n");
System.out.print("Enter your password: ");
do {
boolean invalidLength = false;
boolean containsRestrictedWord = false;
boolean containsLowerCaseLetter = false;
boolean containsUpperCaseLetter = false;
boolean containsDigit = false;
boolean containsSpecialChar = false;
try {
String password = stdIn.nextLine();
for (int i = 0; i < password.length(); i++) {
char ch = password.charAt(i);
if (Character.isUpperCase(ch) && !containsUpperCaseLetter) {
containsUpperCaseLetter = true;
}
if (Character.isLowerCase(ch) && !containsLowerCaseLetter) {
containsLowerCaseLetter = true;
}
if (Character.isDigit(ch) && !containsDigit) {
containsDigit = true;
}
if ((ch == '!' || ch == '@' || ch == '#' || ch == '$' || ch == '%' || ch == '^' || ch == '&'
|| ch == '*') && !containsSpecialChar) {
containsSpecialChar = true;
}
}
if (password.length() < 8) {
invalidLength = true;
}
if (password.contains("and") || password.contains("the")) {
containsRestrictedWord = true;
}
if (invalidLength) {
System.out.println("Invalid: Invalid length");
}
if (containsRestrictedWord) {
System.out.println("Invalid: Contains either the word \"and\" or \"the\".");
}
if (!containsDigit) {
System.out.println("Invalid: Does not contain at least one digit.");
}
if (!containsLowerCaseLetter) {
System.out.println("Invalid: Does not contain at least one lowercase letter.");
}
if (!containsUpperCaseLetter) {
System.out.println("Invalid: Does not contain at least one uppercase letter.");
}
if (!containsSpecialChar) {
System.out.println("Invalid: Does not contain at least one special character.");
}
if (containsSpecialChar && containsUpperCaseLetter && containsLowerCaseLetter && containsDigit
&& !containsRestrictedWord && !invalidLength)
passwordValid = true;
if (passwordValid)
System.out.print("\nPassword is valid.");
}
catch ( Exception ltheXcp ) {
ltheXcp.printStackTrace();
}
} while (!passwordValid);
passwordValid = false;
} while ( 1 != 2 );
// while (!passwordValid) {
// if (passwordValid)
// System.out.print("\nPassword is valid.");
// }
}