java - 在密码验证例程中使用 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;
}
}
解决方案
根据一个值继续循环。而不是使用while(false)
try while(isValid)
whereisValid
是一个布尔值,如果任何条件失败,则为 true,如果所有条件都通过,则为 false。
推荐阅读
- ios - SKStoreReviewController.requestReview() 弹出窗口被挤进去
- javascript - javascript:如何替换会破坏 URL 中查询字符串的特殊符号/字符
- node.js - 如果 NPM 倒闭会怎样?
- python-2.7 - 无法使用 python 客户端删除 gcp 中的 dns 记录
- sql - 我将如何根据所需值(使用 SparkSQL)从位置上选择记录?
- c++ - 重载插入 << 运算符
- python - 如何使按钮更改变量的值?Python tkinter
- c++ - 为什么 GCC 会删除我在 O3 上的代码,而不是在 O0 上?
- python - 为什么我在 Keras 中的 resnet50 模型不收敛?
- r - 如何翻转 pheatmap 中的层次聚类节点