首页 > 解决方案 > 在密码验证例程中使用 if 语句查询 do while 循环

问题描述

我正在尝试创建一个密码验证例程,我的问题是当输入输入时,它会分别通过每个 if 语句。当不满足条件时,我正在尝试重新启动验证。目前,它仅在满足前一个条件时才跳转到下一个 if 语句,即使它是一个新输入。当不满足其中一个条件时,我基本上试图让循环重新运行。目前它单独循环。我尝试了多个 next.() 以尝试回到验证的开始。提前致谢。

 import java.util.Scanner;
 


public class passWordCheck {
 
public static void main(String[] args) {
 
String passWord; // Initialize string for the password.
 
Scanner code = new Scanner(System.in);
// Give requirements for a valid password.
System.out.println("1. A code must contain no spaces");
System.out.println("2. A code must have between 5 and 10 characters (inclusive)");
System.out.println("3. A code must begin with \\ and must end with \\");
System.out.println("4. A code must contain at least one lowercase letter and at least one uppercase letter");
System.out.print("Please enter a code: ");
passWord = code.nextLine();
// Do While loop to check all requirement for a valid password are met.
do {
 
if (!firstCharacter(passWord)) {// Check First Character
    System.out.println("Code must start with \\.");
    System.out.println("Code is invalid, Please try again");
    passWord = code.nextLine();
}
if (!lastCharacter(passWord)) {// Check last Character
    System.out.println("Code must end with \\.");
    System.out.println("Code is invalid, Please try again");
    passWord = code.nextLine();
}
if (!hasCorrectLength(passWord)) {// Check password length
    System.out.println("Code must be between 5 and 15 characters in lenth.");
    System.out.println("Code is invalid, Please try again");
    passWord = code.nextLine();
}
if (!hasUpperCharacter(passWord)) {// Check for uppercase Character
    System.out.println("Code must contain at least one uppercase letter.");
    System.out.println("Code is invalid, Please try again");
    passWord = code.nextLine();
}
if (!hasLowerCharacter(passWord)) {// Check for lowercase character
    System.out.println("Code must contain at least one lowercase letter.");
    System.out.println("Code is invalid, Please try again");
    passWord = code.nextLine();
}
 
} while (false);
 
System.out.println("code is valid.");
code.close();
 
}
 
// Methods for checking password requirements
 
public static boolean firstCharacter(String str) {
 
char first = str.charAt(0); // First character of a string
 
if (first == ('\\')) // This verifies there is a \ at the start.
{
return true;
}
return false;
 
}
 
public static boolean lastCharacter(String str) {
 
int n = str.length(); // Finding string length
 
char last = str.charAt(n - 1); // Last character of a string
 
if (last == ('\\')) // This verifies there is a \ at the end.
{
return true;
}
return false;
 
}
 
public static boolean hasCorrectLength(String str) {
 
int n = str.length(); // Finding string length
 
if (n >= 5 || n <= 15) // This verifies the correct length.
{
return true;
}
 
return false;
}
 
public static boolean hasLowerCharacter(String str) {
 
for (int i = 0; i < str.length(); i++) { // For loop to check password for lowercase
char currentCharacter = str.charAt(i);
if (Character.isLowerCase(currentCharacter)) {
return true;
}
}
return false;
 
}
 
public static boolean hasUpperCharacter(String str) {
 
for (int i = 0; i < str.length(); i++) { // For loop to check password for uppercase
char currentCharacter = str.charAt(i);
if (Character.isUpperCase(currentCharacter)) {
return true;
}
 
}
return false;
 
}
 
}
    

标签: java

解决方案


根据一个值继续循环。而不是使用while(false)try while(isValid)whereisValid是一个布尔值,如果任何条件失败,则为 true,如果所有条件都通过,则为 false。


推荐阅读