密码验证程序 Java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40659911/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:23:15  来源:igfitidea点击:

Password Verification Program Java

java

提问by BlazeRyder

The below are the instructions and my code that I have tried so far. I am almost done but just having problems with part four. So the expected output should be:

以下是我迄今为止尝试过的说明和我的代码。我快完成了,但只是在第四部分有问题。所以预期的输出应该是:

Please enter a password: abc
Password much have at least 8 characters 
Please enter a password: abcd1234$
Password must only contain letter and digits
Please enter a password: ####
Password must have at least 8 characters
Password must only contain letters and digits
Please enter a password: abcd1234
Password accepted!

When I type abcthis is what I get:

当我输入abc 时,这就是我得到的:

Please enter password and then hit enter:abc
Password must have at least 8 characters
Password Accepted

When I do this the program ends!Could someone help me with this?

当我这样做时,程序结束!有人可以帮我解决这个问题吗?

Problem

问题

  1. Write a program that prompts the user to enter a password.
  2. Create a boolean variable named valid and set it to true. If any of these tests below fail, set it to true.
  3. Check the password to see if it has at least 8 characters. If it does not, display the message, "Password must have at least 8 characters"
  4. Check the password to see if it consists of only letter and digits. To do this, you will need to loop through all of the characters in the string. A character c is a letter of digit if this expression is true:

    ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9')

if this is even not true, break from your loop and display the message, "Password must contain only letter and digits" 5. If valid is still true at the end of the program, display the message, "Password accepted!"

  1. 编写一个程序,提示用户输入密码。
  2. 创建一个名为 valid 的布尔变量并将其设置为 true。如果以下任何测试失败,请将其设置为 true。
  3. 检查密码是否至少有 8 个字符。如果不是,则显示消息“密码必须至少有 8 个字符”
  4. 检查密码是否仅由字母和数字组成。为此,您需要遍历字符串中的所有字符。如果此表达式为真,则字符 c 是数字字母:

    ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9')

如果这甚至不正确,则中断循环并显示消息“密码必须仅包含字母和数字” 5. 如果程序结束时有效仍然为真,则显示消息“密码已接受!”

My code

我的代码

    import java.util.Scanner; 
    public class PasswordVerification {

        public static void main(String[] args) {
            //Creates a scanner
            Scanner sc = new Scanner(System.in);
            boolean valid = false; 
            String password;


            //Asks user to enter password
            System.out.print("Please enter password and then hit enter:");
            password = sc.nextLine(); 

            //Checks to see if password is at least 8 characters. 
            if (password.length()<8) 
                {
                    valid = false;
                    System.out.println("Password must have at least 8 characters");
                }

            //Checks each character to see if it is acceptable.
            for (int i = 0; i < password.length(); i++){
                        char c = password.charAt(i);

                        if (       ('a' <= c && c <= 'z') // Checks if it is a lower case letter
                                || ('A' <= c && c <= 'Z') //Checks if it is an upper case letter
                                || ('0' <= c && c <= '9') //Checks to see if it is a digit
                        ) 
                        {

                            valid = true;
                        } 

                        else 
                        {
                            // tells the user that only letters & digits are allowed
                            System.out.println("Only letter & digits are acceptable.");
                            valid = false;
                            break;
                        }

            }

            // if the password is valid, tell the user it's accepted
            System.out.println("Password Accepted");
            }


    }

回答by aleb2000

As @cralfaro stated you have to repeat the process if the password is invalid:

正如@cralfaro 所说,如果密码无效,您必须重复该过程:

import java.util.Scanner; 
public class PasswordVerification {

    public static void main(String[] args) {
        //      Creates a scanner
        Scanner sc = new Scanner(System.in);
        boolean valid = false; 
        String password;

        do { // start a loop
            //      Asks user to enter password
            System.out.print("Please enter password and then hit enter:");
            password = sc.nextLine(); 

            //      Checks to see if password is at least 8 characters. 
            if (password.length()<8) 
            {
                valid = false;
                System.out.println("Password must have at least 8 characters");
                continue; // skip to next iteration
            }

            //      Checks each character to see if it is acceptable.
            for (int i = 0; i < password.length(); i++){
                char c = password.charAt(i);

                if (       ('a' <= c && c <= 'z') // Checks if it is a lower case letter
                        || ('A' <= c && c <= 'Z') //Checks if it is an upper case letter
                        || ('0' <= c && c <= '9') //Checks to see if it is a digit
                ) 
                {

                    valid = true;
                }
                else 
                {
                    // tells the user that only letters & digits are allowed
                    System.out.println("Only letter & digits are acceptable.");
                    valid = false;
                    break;
                }

            }
        } while(!valid); // verify if the password is valid, if not repeat the process

        // if the password is valid, tell the user it's accepted
        System.out.println("Password Accepted");
    }


}

In this way the program will continue to ask the user input if the password is not valid.

这样,如果密码无效,程序将继续询问用户输入。

EDIT:Thanks to GC_'s comment, the problem was that I missed a continue statement in the first check.

编辑:感谢 GC_ 的评论,问题是我在第一次检查中错过了 continue 语句。

回答by Aracurunir

Your problem ist this part of code:

你的问题是这部分代码:

if (    ('a' <= c && c <= 'z') // Checks if it is a lower case letter
     || ('A' <= c && c <= 'Z') //Checks if it is an upper case letter
     || ('0' <= c && c <= '9') //Checks to see if it is a digit
    ) { valid = true; }

Here you reset validto true, even if the first check with the length being less than 8already failed. So abcwill fail 3.and print Password must have at least 8 characterswhich is fine. Then it will pass 4., reset valid to trueand print Password Accepted.

在这里您重置validtrue,即使第一次检查长度小于8已经失败。所以abc会失败3.并打印Password must have at least 8 characters这很好。然后它将通过4.,将有效重置为true并打印Password Accepted

So you want the replace the if (...) { valid = true; } else { ... }part by

所以你想要替换if (...) { valid = true; } else { ... }部分

if (!( // if the character is none of the options below, print error
       ('a' <= c && c <= 'z') // Checks if it is a lower case letter
    || ('A' <= c && c <= 'Z') //Checks if it is an upper case letter
    || ('0' <= c && c <= '9') //Checks to see if it is a digit
    )) { 
        // tells the user that only letters & digits are allowed
        System.out.println("Only letter & digits are acceptable.");
        valid = false;
        break; 
    }

Edit: Also you should initially set boolean valid = true;instead of false, as you want to set it to falseonly if it fails a condition. Then at the end of your code, add a condition checking validaround the last output line, like

编辑:此外,您应该最初设置boolean valid = true;而不是false,因为您只想false在它失败的情况下将其设置为。然后在代码的末尾,valid在最后一个输出行周围添加一个条件检查,例如

if(valid) {
    System.out.println("Password Accepted");
}

回答by Cristophs0n

A little messy but try this:

有点乱,但试试这个:

public static void main(String[] args) {
//      Creates a scanner
Scanner sc = new Scanner(System.in);
boolean valid = false; 
String password;


//      Asks user to enter password
while(true){
System.out.print("Please enter password and then hit enter:");
password = sc.nextLine(); 

//      Checks to see if password is at least 8 characters. 
if (password.length()<8) 
    {
        valid = false;
        System.out.println("Password must have at least 8 characters");
    }
else {

//      Checks each character to see if it is acceptable.
for (int i = 0; i < password.length(); i++){
            char c = password.charAt(i);

            if (       ('a' <= c && c <= 'z') // Checks if it is a lower case letter
                    || ('A' <= c && c <= 'Z') //Checks if it is an upper case letter
                    || ('0' <= c && c <= '9') //Checks to see if it is a digit
            ) {
                valid = true;
            } else {
                System.out.println("Password denied");
                System.out.println("Only letter & digits are acceptable.");
                valid = false;
                break;
            }


}

if (valid == true) {
System.out.println("Password accepted");
    break;
        }            
}
}
}

回答by Joe

Add a check to see if the password is still valid before printing that the password is accepted.

在打印密码被接受之前添加检查以查看密码是否仍然有效。

if(valid==true) {
    System.out.println("Password Accepted");
}

EDIT: You can also add this.

编辑:你也可以添加这个。

else {
    System.out.println("Password Denied");
}