Java密码检查
我遇到的问题是让它在确认密码之前重复第一个密码过程Java密码检查,java,Java,我遇到的问题是让它在确认密码之前重复第一个密码过程 //... // ask for password System.out.print("Password : "); inputPassword = input.next(); if(!passCheck(inputPassword)) { return; } 密码长度必须至少为8个字符 密码必须至少包含以下内容: 一个字母字符[a-zA-Z] 一个数字字符[0-9] 非字母或数字字符,如 !#@$%^&*( ) - _ = + [
//...
// ask for password
System.out.print("Password : ");
inputPassword = input.next();
if(!passCheck(inputPassword)) {
return;
}
密码长度必须至少为8个字符
密码必须至少包含以下内容:
- 一个字母字符
[a-zA-Z]
- 一个数字字符
[0-9]
- 非字母或数字字符,如
!#@$%^&*( ) - _ = + [ ] ; : ' “,<.>/?
- 包含空格
- 以感叹号
或问号[!]
[?]
public static void main(String[] args) {
//declare variables
String inputPassword; // variable for password
String confirmPassword;
// set up input stream from the keyboard
Scanner input = new Scanner (System.in);
// ask for password
System.out.print("Password : ");
inputPassword = input.next();
passCheck(inputPassword);
System.out.print("Please confirm your password : ");
confirmPassword = input.next();
if(inputPassword.matches(confirmPassword)){
System.out.println("Password successfully created.");
} else {
System.out.println("Passwords do not match.");
}
}
public static void passCheck(String password){
boolean valid = true;
if(password.length() < 8){
System.out.println("Password is not eight characters long.");
valid = false;
}
String upperCase = "(.*[A-Z].*)";
if(!password.matches(upperCase)){
System.out.println("Password must contain at least one capital letter.");
valid = false;
}
String numbers = "(.*[0-9].*)";
if(!password.matches(numbers)){
System.out.println("Password must contain at least one number.");
valid = false;
}
String specialChars = "(.*[ ! # @ $ % ^ & * ( ) - _ = + [ ] ; : ' \" , < . > / ?].*)";
if(!password.matches(specialChars)){
System.out.println("Password must contain at least one special character.");
valid = false;
}
String space = "(.*[ ].*)";
if(password.matches(space)){
System.out.println("Password cannot contain a space.");
valid = false;
}
if(password.startsWith("?")){
System.out.println("Password cannot start with '?'.");
valid = false;
}
if(password.startsWith("!")){
System.out.println("Password cannot start with '!'.");
valid = false;
}
if(valid){
System.out.println("Password is valid.");
}
}
publicstaticvoidmain(字符串[]args){
//声明变量
字符串inputPassword;//密码的变量
字符串确认密码;
//从键盘设置输入流
扫描仪输入=新扫描仪(System.in);
//询问密码
系统输出打印(“密码:”);
inputPassword=input.next();
密码检查(输入密码);
System.out.print(“请确认您的密码:”);
confirmPassword=input.next();
if(inputPassword.matches(confirmPassword)){
System.out.println(“成功创建密码”);
}否则{
System.out.println(“密码不匹配”);
}
}
公共静态无效密码检查(字符串密码){
布尔有效=真;
if(password.length()<8){
System.out.println(“密码不是八个字符长。”);
有效=错误;
}
字符串大写=“(.[A-Z].”;
如果(!password.matches)(大写)){
System.out.println(“密码必须至少包含一个大写字母。”);
有效=错误;
}
字符串编号=“(.[0-9].”;
如果(!password.matches(数字)){
System.out.println(“密码必须至少包含一个数字”);
有效=错误;
}
字符串specialChars=“(.[!\@$%^&*()-\=+[];:”\,<.>/?]);
如果(!password.matches(specialChars)){
System.out.println(“密码必须至少包含一个特殊字符。”);
有效=错误;
}
字符串空格=“(.[].*)”;
if(password.matches(空格)){
System.out.println(“密码不能包含空格。”);
有效=错误;
}
if(password.startsWith(“?”){
System.out.println(“密码不能以“?”开头”);
有效=错误;
}
if(password.startsWith(“!”){
System.out.println(“密码不能以“!”开头”);
有效=错误;
}
如果(有效){
System.out.println(“密码有效”);
}
}
我得到的是,在它告诉我其中一个问题后,它会要求确认密码,这不是我想要的。如果密码无效,用户无需重复密码 我对此的看法
System.out.print("Password : ");
inputPassword = input.next();
if(passCheck(inputPassword))
{
System.out.print("Please confirm your password : ");
confirmPassword = input.next();
if(inputPassword.matches(confirmPassword)){
System.out.println("Password successfully created.");
} else {
System.out.println("Passwords do not match.");
}
}
else {....}
编辑:我注意到您的方法passCheck的返回类型是void。尝试将其更改为布尔值您可以将
passCheck()
方法的返回类型更改为布尔值
,并在末尾返回其本地有效
变量的值:
public static boolean passCheck(String password){
// ...
if(valid){
System.out.println("Password is valid.");
}
return valid;
}
然后在main()
//...
// ask for password
System.out.print("Password : ");
inputPassword = input.next();
if(!passCheck(inputPassword)) {
return;
}
首先,如果密码格式不正确,您永远不会告诉代码中断。我建议将passCheck()方法的返回类型更改为boolean,并添加
return valid;
到最后
然后,使用do while循环持续询问密码,直到密码匹配,例如:
do{
System.out.print("Password : ");
inputPassword = input.next();
}while(!passCheck(inputPassword));
这将确认在密码格式正确之前程序不会继续。您可以将[^A-Za-z0-9]
用于非字母数字。从passCheck返回密码的有效性(例如布尔值)。因此,大体上,您可以在继续之前测试布尔值。带空格、问号和感叹号的密码比不带空格、问号和感叹号的密码强。结尾处的else会在if位于上方时提示我一个错误,说“else”而不说“if”。对不起,请删除ifalso结尾处的分号,如果您对我的答案满意,请单击“检查”按钮。让我知道你是否需要更多的帮助