编写程序以检查密码是否包含特定内容

huangapple go评论62阅读模式
英文:

Writing a program to check password for certain things

问题

我正在编写一个程序,以检查用户提供的密码中是否包含特定值。我已经发布了下面的代码,我认为一切都很好,只是对于 if 语句中布尔检查的语法应该如何检查感到困惑。对任何帮助表示感谢!

public class PasswordValidation
{
    public static void main(String[] args){
        Scanner stdin = new Scanner(System.in);
        System.out.println("Password: ");
        String password = stdin.next();
        int passwordLength = password.length();

        String pCheck = "password";

        boolean capital;
        boolean lowercase;
        boolean number;
        boolean specialChar;
        char check;
        for(int i = 0; i < password.length(); i++){
            check = password.charAt(i);
            if(Character.isDigit(check)){
                number = true;
            }else if (Character.isUpperCase(check)){
                capital = true;
            }else if (Character.isLowerCase(check)){
                lowercase = true;
            }else{
                specialChar = true;
            }
        }
        if(number = true && capital = true && lowercase = true && specialChar = false && password.length() >= 8 && password.toLowerCase().contains(pCheck.toLowerCase())){
            System.out.println("Password is good");
英文:

Im writing a program to check for values in a password given by the user. I have posted the code below and I think its all good I'm just confused on how the syntax for the boolean check in the if statement should be checked. Any help is appreciated, thanks!

public class PasswordValidation
{
public static void main(String[] args){
    Scanner stdin = new Scanner(System.in);
    System.out.println(&quot;Password: &quot;);
    String password = stdin.next();
    int passwordLength = password.length();

    String pCheck = &quot;password&quot;;

    boolean capital;
    boolean lowercase;
    boolean number;
    boolean specialChar;
    char check;
    for(int i = 0; i &lt; password.length(); i++){
        check = password.charAt(i);
        if(Character.isDigit(check)){
            number = true;
        }else if (Character.isUpperCase(check)){
            capital = true;
        }else if (Character.isLowerCase(check)){
            lowercase = true;
        }else{
            specialChar = true;
        }
    }
    if(number = true &amp;&amp; capital = true &amp;&amp; lowercase = true &amp;&amp; specialChar = false &amp;&amp; password.length() &gt;= 8 &amp;&amp; password.toLowerCase().contains(pCheck.toLowerCase())){
        System.out.println(&quot;Password is good&quot;);

答案1

得分: 1

你有一些错误,要检查值的相等性,你需要使用 == 而不是 =

number == true && capital == true && lowercase == true ...

或者更好的做法是

number && capital && lowercase ...

因为它们只是布尔值。

英文:

you have some errors, to check equality of value you need to use == not =

number == true &amp;&amp; capital == true &amp;&amp; lowercase == true ...

or better you can do this

number &amp;&amp; capital &amp;&amp; lowercase ...

because they are just booleans

huangapple
  • 本文由 发表于 2020年10月22日 02:45:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/64469827.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定