简体   繁体   English

为什么跳过if-else语句的else部分?

[英]Why is it skipping the else part of my if-else statement?

It keeps skipping the else part of my statement. 它一直在跳过我声明的其他部分。 I just want it to print out of the invalid line if all the other restrictions are not met. 如果不满足所有其他限制,我只希望它从无效行中打印出来。

import java.util.Scanner;
public class Program_3
{
   public static void main(String[] args)
   {
      Scanner input = new Scanner(System.in);
      System.out.print("Enter a password: ");
      String str = input.nextLine();
      boolean lowerCase = false;
      boolean upperCase = false;
      boolean number = false;
      char y;
      for (int i = 0; i < str.length(); i++)
      {
         y = str.charAt(i);
         if(Character.isUpperCase(y))
         {
            upperCase = true;
         }
         else if(Character.isDigit(y))
         {
            number = true;
         }
         else if(Character.isLowerCase(y))
         {
            lowerCase = true;
         }
      }
      if (lowerCase == true)
      {
         if (upperCase == true)
         {
            if (number == true)
            {
               if (str.length() >= 8)
               {
                  System.out.println("Verdict:\t Valid");
               }
            }
         }
      }
      else
         System.out.println("Verdict:\t Invalid");
   }
}

why does it skip and not print the invalid line when all ifs are not met? 如果不满足所有条件,为什么会跳过而不打印无效行?

The else in your code relates only to the outermost if . 代码中的else仅与最外部的 if So it will only be executed if lowerCase == false . 因此,只有在lowerCase == false时才执行。

To fix this logic, combine all three conditions in a single if , ie: 要解决此逻辑,请将所有三个条件合并为一个if ,即:

  if (lowerCase == true && upperCase == true && number == true && str.length() >= 8)
  {
      System.out.println("Verdict:\t Valid");
  }
  else
     System.out.println("Verdict:\t Invalid");

Side note, booleans don't require explicit comparison to true , so you can write it shorter: 旁注,布尔值不需要与true进行显式比较,因此可以将其写得更短:

      if (lowerCase && upperCase && number && str.length() >= 8)

The else condition is placed in the wrong place, it'll only be reached if the lowerCase condition is false . else条件放置在错误的位置,只有在lowerCase条件为false才能达到。 And anyway, we can simplify and combine all the if s in a single condition, what you meant to say was this: 而且无论如何,我们可以在单个条件下简化和组合所有if ,您的意思是:

if (lowerCase && upperCase && number && str.length() >= 8) {
   System.out.println("Verdict:\t Valid");
} else {
   System.out.println("Verdict:\t Invalid");
}

This code can b be simplified to show control-flow: 可以简化此代码以显示控制流:

  if(lowerCase == true)
  {
      //lowerCase == true -> execute this code
      if( upperCase == true)...
  }else
      //lowerCase == false -> execute this code
      ...

The inner if-statements (which are exclusive btw.) don't execute the outer else -statements, if the condition is false. 如果条件为false,则内部if语句(排他的btw。)不会执行外部else语句。 The logical correct version of your code would be: 您的代码的逻辑正确版本为:

if(lowerCase && upperCase && isNumber && str.length() > 8)
    System.out.println("Verdict:\t Valid");
else
    ...

Your test password contains at least one lowercase character. 您的测试密码至少包含一个小写字符。 As long as it does, the else statement will never execute. 只要这样做,else语句就永远不会执行。 To test "if all conditions are true ... else ..." try the follwing: 要测试“如果所有条件都成立...否则...”,请尝试以下方法:

if (lowerCase && upperCase && number && str.length >= 8) {
    // password ok
} else {
    // password not ok
}

By the way, as you can see I don't use lowerCase == true since it's not needed, lowerCase already is a boolean. 顺便说一句,如您所见,由于不需要,我不使用lowerCase == true ,lowerCase已经是一个布尔值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM