java - 如何让我的代码继续接收用户输入,直到密码与参数匹配?
问题描述
我试图让我的代码循环,直到用户输入符合密码参数“请输入具有以下条件的密码:密码必须包含至少 8 个字符、1 个大写字母、1 个小写字母、1 个数字
Password must not contain the word 'password' , spaces (' ') or special characters");
我正在努力让我的 do-while 循环工作。任何帮助将非常感激。
代码片段:
import java.util.Scanner;
public class PasswordValidation
{
public static void main(String[]args){
boolean valid = false;
System.out.println("Please enter password with the following criteria");
System.out.println("Password must contain atleast 8 characters, 1 uppercase letter, 1 lowercase letter,1 number");
System.out.println("Password must not contain the word 'password' , spaces (' ') or special characters");
do {
Scanner input = new Scanner(System.in);
String password = input.nextLine();
if (password.length() >= 8)
{
System.out.println("Password must be atleast 8 characters in length.");
valid = false;
}
String upperCaseChars = "(.*[A-Z].*)";
if (!password.matches(upperCaseChars ))
{
System.out.println("Password should contain atleast one upper case letter");
valid = false;
}
String lowerCaseChars = "(.*[a-z].*)";
if (!password.matches(lowerCaseChars ))
{
System.out.println("Password should contain atleast one lower case letter");
valid = false;
}
String num = "(.*[0-9].*)";
if (!password.matches(num ))
{
System.out.println("Password should contain atleast one number.");
valid = false;
}
String specialChar = "(.*[,~,!,@,#,$,%,^,&,*,(,),-,_,=,+,[,{,],},|,;,:,<,>,/,?].*$)";
if (password.matches(specialChar ))
{
System.out.println("Password should not contain special character");
valid = false;
}
if (password == "password")
{
System.out.println("Password Should not be 'password'");
valid = false;
}
else
{
valid = true;
System.out.println("Password is valid.");
}
}
} while valid = false;
}
解决方案
如果您在 do-while 循环之前存储所有 String 值,则需要更少的计算时间。因为它不需要每次都初始化字符串变量。而且,如果你使用 else-if 阶梯,那么它也会减少执行时间。它应该是这样的:
Scanner input = new Scanner(System.in);
boolean valid;
String upperCaseChars = "(.*[A-Z].*)";
String lowerCaseChars = "(.*[a-z].*)";
String num = "(.*[0-9].*)";
String specialChar = "(.*[,~,!,@,#,$,%,^,&,*,(,),-,_,=,+,[,{,],},|,;,:,<,>,/,?].*$)";
System.out.println("Please enter password with the following criteria");
System.out.println("Password must contain atleast 8 characters, 1 uppercase letter, 1 lowercase letter,1 number");
System.out.println("Password must not contain the word 'password' , spaces (' ') or special characters");
do {
String password = input.nextLine();
if (password.length() < 8) {
System.out.println("Password must be atleast 8 characters in length.");
valid = false;
}
else if (!password.matches(upperCaseChars)) {
System.out.println("Password should contain atleast one upper case letter");
valid = false;
}
else if (!password.matches(lowerCaseChars)) {
System.out.println("Password should contain atleast one lower case letter");
valid = false;
}
else if (!password.matches(num)) {
System.out.println("Password should contain atleast one number.");
valid = false;
}
else if (password.matches(specialChar)) {
System.out.println("Password should not contain special character");
valid = false;
}
else if (password == "password") {
System.out.println("Password Should not be 'password'");
valid = false;
} else {
valid = true;
System.out.println("Password is valid.");
}
}
while (valid == false);
推荐阅读
- reactjs - 使用 react 和 redux/thunk 进行异步操作后的导航
- android - 磁盘上的android avd大小如何小于实际大小
- mysql - MySQL Left Join 4 个表,其中包含空重复行
- javascript - 使用 JavaScript 解析逗号分隔的字符串?
- javascript - Nodejs - DNS.Lookup 拒绝使用 HTTP 的 URL?
- php - 根据 SQL 读取结果生成非线性 html 表
- android - 带有谷歌地图的 AsyncTask OnPostExecute 中的 NullPointerException
- python - MySQL 连接错误 Python、Flask、MySQL
- c++ - 树插入/删除/打印崩溃
- python-3.x - Python 3.6//Django 2.1.4//模板:for 循环对用户不起作用