Java 创建一个要求输入密码并检查密码是否正确的程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24661486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a program that asks for a password and checks if its correct or not
提问by user3821797
this is some practice i am doing for my midterm tomorrow. I cant seem to find the problem with my code, this is the question:
这是我明天为期中考试做的一些练习。我似乎无法找到我的代码的问题,这是问题:
Having a secure password is a very important practice, when much of our information is stored online. Write a program that validates a new password, following these rules:
? The password must be at least 8 characters long.
? The password must have at least one uppercase and one lowercase letter
? The password must have at least one digit.
Write a program that asks for a password, then asks again to confirm it. If the passwords don't match or the rules are not fulfilled, prompt again. Your program should include a method that checks whether a password is valid.
当我们的大部分信息都在线存储时,拥有安全密码是一项非常重要的做法。编写一个验证新密码的程序,遵循以下规则:
? 密码长度必须至少为 8 个字符。
? 密码必须至少有一个大写和一个小写字母
? 密码必须至少有一位。
编写一个程序,要求输入密码,然后再次要求确认。如果密码不匹配或不符合规则,则再次提示。您的程序应该包含一个检查密码是否有效的方法。
Here is the code:
这是代码:
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;
}
}
}
Error is:
错误是:
I enter a name less than 8 digits and it would say its valid when its not, also when its all valid and right, it doesn't print that its valid.
我输入的名称少于 8 位数字,它会说它有效,当它无效时,同样当它全部有效且正确时,它不会打印它的有效。
回答by Eran
instead of
代替
while (!password.equals(confirm) && (!condition)) {
you want
你要
while (!password.equals(confirm) || (!condition)) {
Since the password is invalid if either isValid(password)
returned false, or the confirmed password is different than the original typed password.
由于如果isValid(password)
返回 false 或确认的密码与原始键入的密码不同,则密码无效。
Your current code won't pass the while condition if password.equals(confirm)
, even though condition is false (due to the password being to short).
您当前的代码不会通过 while 条件 if password.equals(confirm)
,即使条件为假(由于密码较短)。
In addition, your while loop should call condition = isValid(password);
again after the password and confirmed password are re-typed. Otherwise, the while loop would never exit.
此外,您的 while 循环应该condition = isValid(password);
在重新输入密码和确认密码后再次调用。否则,while 循环永远不会退出。
So either write :
所以要么写:
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);
}
Or more elegantly :
或者更优雅:
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();
}
回答by Gian
There might be a few issues here, but the one that stands out is this:
这里可能有一些问题,但最突出的是:
while (!password.equals(confirm) && (!condition)) {
If I type the password "abc" and confirmation as "abc", then !password.equals(confirm)
is false
. I think you want this to be:
如果我输入密码“abc”并确认为“abc”,则!password.equals(confirm)
是false
. 我想你希望这是:
while (!password.equals(confirm) || (!condition)) {
Because you want to repeat this behavior whenever the password doesn't match oris invalid.
因为您想在密码不匹配或无效时重复此行为。
This is also incorrect:
这也是不正确的:
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();
}
You never re-check isValid
, and you're storing the re-entered password and confirmation in new variables (scoped to the loop) called Password
and Confirm
. Try assigning these directly to your existing variables and re-calling isValid
:
您永远不会重新检查isValid
,并且您将重新输入的密码和确认存储在名为Password
and 的新变量(范围为循环)中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
}
回答by Greg Hilston
@user3821797: I slightly modified your isValid method. Go through it and make sure you understand what I did. If you have any questions at all, please comment here and I'll help you out.
@ 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
}
回答by Boga Srinivas
I have checked above code and went through small modification and have tested over it and it is not accepting the password with less than 8 digits.And no need to add more variables. Go through it..
我已经检查了上面的代码并进行了小的修改并对其进行了测试,它不接受少于 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);
}