返回正确的布尔值存在问题

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

Trouble returning the correct boolean value

问题

import java.util.Scanner;

public class Password {
    public static void main(String[] args) {
        String another = "y";
        while (another.equalsIgnoreCase("y")) {
            Scanner scan = new Scanner(System.in);
            System.out.print("Enter a password: ");
            String password = scan.nextLine();

            if (checkPassword(password)) {
                System.out.println("Entered Password: " + password);
                System.out.println("Judgement: " + "Valid password");
                System.out.println("Re-run Program (y/n)? ");
                another = scan.next();
            } else {
                System.out.println("Entered Password: " + password);
                System.out.println("Judgement: " + "Invalid password");
                System.out.println("Re-run Program (y/n)? ");
                another = scan.next();
            }
        }
    }

    public static boolean checkPassword(String password) {
        if (!minChars(password)) {
            return false;
        } else if (!onlyCharsAndDigits(password)) {
            return false;
        } else if (!minDigits(password)) {
            return false;
        } else if (!minLowerCase(password)) {
            return false;
        } else if (!minUpperCase(password)) {
            return false;
        }
        return true;
    }

    public static boolean minChars(String password) {
        if (password.length() > 8) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean onlyCharsAndDigits(String password) {
        for (int i = 0; i < password.length(); i++) {
            if (!Character.isLetter(password.charAt(i)) && !Character.isDigit(password.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    public static boolean minDigits(String password) {
        int numberOfDigits = 0;
        for (int i = 0; i < password.length(); i++) {
            if (Character.isDigit(password.charAt(i))) {
                numberOfDigits++;
            }
        }
        return numberOfDigits >= 3;
    }

    public static boolean minLowerCase(String password) {
        int numberOfLowerCase = 0;
        for (int i = 0; i < password.length(); i++) {
            if (Character.isLowerCase(password.charAt(i))) {
                numberOfLowerCase++;
            }
        }
        return numberOfLowerCase >= 3;
    }

    public static boolean minUpperCase(String password) {
        int numberOfUpperCase = 0;
        for (int i = 0; i < password.length(); i++) {
            if (Character.isUpperCase(password.charAt(i))) {
                numberOfUpperCase++;
            }
        }
        return numberOfUpperCase >= 3;
    }
}
英文:

I am trying to create a program to check a password. All 5 conditions must be true for the password to be valid. The problem I am having is if the entered password is long enough, the password is automatically valid even if the other conditions are not valid. I have tried changing many small things, but haven't been able to get it to check all the conditions. Any help is much appreciated!

public class Password
{
//Program main method
public static void main(String [] args)
{
String another = &quot;y&quot;;
while (another.equalsIgnoreCase(&quot;y&quot;))
{
Scanner scan = new Scanner(System.in);
System.out.print(&quot;Enter a password: &quot;);
String password = scan.nextLine();
//Print output
if (checkPassword(password))
{
System.out.println(&quot;Entered Password: &quot; + password);
System.out.println(&quot;Judgement: &quot; + &quot;Valid password&quot;);
System.out.println(&quot;Re-run Program (y/n)? &quot;);
another = scan.next();
}
else 
{
System.out.println(&quot;Entered Password: &quot; + password);
System.out.println(&quot;Judgement: &quot; + &quot;Invalid password&quot;);
System.out.println(&quot;Re-run Program (y/n)? &quot;);
another = scan.next();
}
}
}
//
public static boolean checkPassword(String password)
{
if(!minChars(password)) 
{
return false;
}
else if (!onlyCharsAndDigits(password)) 
{
return false;
}
else if (!minDigits(password)) 
{
return false;
}
else if (!minLowerCase(password)) 
{
return false;
}
else if (!minUpperCase(password)) 
{
return false;
}
return true;
}
//
public static boolean minChars(String password)
{
if (password.length() &gt; 8) {
return true;
}
else {
return false;
}
}
//method to test password has letters and digits or not
public static boolean onlyCharsAndDigits(String password)
{
for(int i=0;i &lt; password.length();i++)
{
if(!Character.isLetter(password.charAt(i)) &amp;&amp; !Character.isDigit(password.charAt(i)))
{
return false;
}
}
return true;
}
//method to test password has letters and digits or not
public static boolean minDigits(String password)
{
int numberOfDigits = 0;
for(int i=0;i &lt; password.length();i++)
{
if(Character.isDigit(password.charAt(i)));
{
numberOfDigits++;
}
}
return numberOfDigits &gt;= 3;
}
//method to test password has letters and digits or not
public static boolean minLowerCase(String password)
{
int numberOfLowerCase = 0;
for(int i=0;i &lt; password.length();i++)
{
if(Character.isLowerCase(password.charAt(i)));
{
numberOfLowerCase++;
}
}
return numberOfLowerCase &gt;= 3;
}
//method to test password has letters and digits or not
public static boolean minUpperCase(String password)
{
int numberOfUpperCase = 0;
for(int i=0;i &lt; password.length();i++)
{
if(!Character.isUpperCase(password.charAt(i)));
{
numberOfUpperCase++;
}
}
return numberOfUpperCase &gt;= 3;
}
}

答案1

得分: 2

checkPassword方法中的所有else if更改为if

如果执行了任何前面的分支,else if分支将不会被执行。

您的代码可以简化为

public static boolean checkPassword(String password) {
    return minChars(password) && onlyCharsAndDigits(password) 
        && minDigits(password) && minLowerCase(password) && minUpperCase(password);
}
英文:

Change all else if to if in the checkPassword method.

An else if branch will not be executed if any of the preceding branches are executed.

Your code can be simplified as

public static boolean checkPassword(String password) {
return minChars(password) &amp;&amp; onlyCharsAndDigits(password) 
&amp;&amp; minDigits(password) &amp;&amp; minLowerCase(password) &amp;&amp; minUpperCase(password);
}

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

发表评论

匿名网友

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

确定