这是我为明年中期做的一些练习。我似乎无法找到我的代码的问题,这是一个问题:
在我们的大部分信息中,拥有安全密码是非常重要的做法 在线存储。编写一个验证新密码的程序,然后执行以下操作 规则:
•密码长度必须至少为8个字符。
•密码必须至少包含一个大写和一个小写字母
•密码必须至少有一位数。
编写一个要求输入密码的程序,然后再次要求确认。如果 密码不匹配或规则未满足,再次提示。你的计划 应该包括一个检查密码是否有效的方法。
以下是代码:
import java.util.Scanner;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author HP Laptop
*/
public class PasswordValidation {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Please enter password : ");
String password = in.nextLine();
System.out.print("Please re-enter the password to confirm : ");
String confirm = in.nextLine();
boolean condition;
condition = isValid(password);
while (!password.equals(confirm) && (!condition)) {
System.out.println("The password is invalid");
System.out.print("Please enter the password again : ");
String Password = in.nextLine();
System.out.print("Please re-enter the password to confirm : ");
String Confirm = in.nextLine();
}
if (isValid(password)) {
System.out.println("The password is valid");
/*} else {
System.out.println("The password is not valid");
System.out.print("Please enter the password again : ");
String Password = in.nextLine();
System.out.print("Please re-enter the password to confirm : ");
String Confirm = in.nextLine();
}*/
}
}
public static boolean isValid(String password) {
if (password.length() < 8) {
return false;
} else {
for (int i = 0; i < password.length(); i++) {
if (Character.isUpperCase(password.charAt(i))) {
}
}
for (int b = 0; b < password.length(); b++) {
if (Character.isLowerCase(password.charAt(b))) {
}
}
for (int c = 0; c < password.length(); c++) {
if (Character.isDigit(password.charAt(c))) {
}
}
return true;
}
}
}
错误是:
我输入一个小于8位的名字,当它不是时,它会说它有效,当它全部有效且正确时,它不会打印出它的有效数字。
答案 0 :(得分:1)
@ user3821797:我稍微修改了你的isValid方法。仔细检查,确保你明白我做了什么。如果您有任何疑问,请在此处发表评论,我会帮助您。
public static boolean isValid(String password) {
Boolean atleastOneUpper = false;
Boolean atleastOneLower = false;
Boolean atleastOneDigit = false;
if (password.length() < 8) { // If its less then 8 characters, its automatically not valid
return false;
}
for (int i = 0; i < password.length(); i++) { // Lets iterate over only once. Saving time
if (Character.isUpperCase(password.charAt(i))) {
atleastOneUpper = true;
}
else if (Character.isLowerCase(password.charAt(i))) {
atleastOneLower = true;
}
else if (Character.isDigit(password.charAt(i))) {
atleastOneDigit = true;
}
}
return (atleastOneUpper && atleastOneLower && atleastOneDigit); // Return true IFF the password is atleast eight characters long, has atleast one upper, lower and digit
}
答案 1 :(得分:0)
而不是
while (!password.equals(confirm) && (!condition)) {
你想要
while (!password.equals(confirm) || (!condition)) {
如果isValid(password)
返回false,或者确认的密码与原始输入的密码不同,则密码无效。
如果password.equals(confirm)
,您的当前代码将不会通过while条件,即使条件为false(由于密码短路)。
此外,在重新输入密码和确认密码后,您的while循环应再次调用condition = isValid(password);
。否则,while循环将永远不会退出。
所以要么写:
condition = isValid(password);
while (!password.equals(confirm) || (!condition)) {
System.out.println("The password is invalid");
System.out.print("Please enter the password again : ");
String Password = in.nextLine();
System.out.print("Please re-enter the password to confirm : ");
String Confirm = in.nextLine();
condition = isValid(password);
}
或更优雅:
while (!password.equals(confirm) || !isValid(password)) {
System.out.println("The password is invalid");
System.out.print("Please enter the password again : ");
String Password = in.nextLine();
System.out.print("Please re-enter the password to confirm : ");
String Confirm = in.nextLine();
}
答案 2 :(得分:0)
这里可能存在一些问题,但突出的问题是:
while (!password.equals(confirm) && (!condition)) {
如果我输入密码“abc”并确认为“abc”,则!password.equals(confirm)
为false
。我想你想要这样:
while (!password.equals(confirm) || (!condition)) {
因为您想在密码不匹配时重复此行为或无效。
这也是错误的:
condition = isValid(password);
while (!password.equals(confirm) && (!condition)) {
System.out.println("The password is invalid");
System.out.print("Please enter the password again : ");
String Password = in.nextLine();
System.out.print("Please re-enter the password to confirm : ");
String Confirm = in.nextLine();
}
您永远不会重新检查isValid
,并且您将重新输入的密码和确认存储在名为Password
和Confirm
的新变量(范围为循环)中。尝试将这些直接分配给现有变量并重新调用isValid
:
condition = isValid(password);
while (!password.equals(confirm) && (!condition)) {
System.out.println("The password is invalid");
System.out.print("Please enter the password again : ");
password = in.nextLine(); // <-- HERE
System.out.print("Please re-enter the password to confirm : ");
confirm = in.nextLine(); // <-- HERE
condition = isValid(password); // <-- HERE
}
答案 3 :(得分:0)
我检查了上面的代码并进行了较小的修改并进行了测试,它不接受少于8位数字的密码,并且无需添加更多变量。 经历一下。
condition = isValid(password);
while (!password.equals(confirm) && (!condition)) {
System.out.println("The password is invalid");
System.out.print("Please enter the password again : ");
String Password = in.nextLine();
System.out.print("Please re-enter the password to confirm : ");
String Confirm = in.nextLine();
password = Password;
confirm = Confirm;
condition = isValid(password);
}