无法返回正确的布尔值

时间:2020-09-21 18:15:10

标签: java

我正在尝试创建一个程序来检查密码。要使密码有效,必须满足所有5个条件。我遇到的问题是,如果输入的密码足够长,即使其他条件无效,该密码也会自动有效。我曾尝试更改许多小东西,但无法检查所有条件。任何帮助深表感谢!

public class Password
{
//Program main method
   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();
      
      
      //Print output
         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;
      }
   }

//method to test password has letters and digits or not
   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;
   }


//method to test password has letters and digits or not
   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;
   }

//method to test password has letters and digits or not

   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;
   }

//method to test password has letters and digits or not
   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;
   }
}

1 个答案:

答案 0 :(得分:2)

使用else if方法将所有if更改为checkPassword

如果执行任何上述分支,则不会执行else if分支。

您的代码可以简化为

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