检查if语句中所有条件的最有效方法是什么?

时间:2017-05-14 03:58:50

标签: java algorithm

所以我试图这样做,如果多条件的声明,为了使密码合格,它应该满足5个中的4个。我收到错误说

运营商&&未定义参数类型boolean,int

注意:错误发生在我的方法的底部,(如果声明错误)

      public void passRequirments(){


        int digit,symbol,upper,lower;
        int countDigit, countSymbol,countUpper,countLower;
        countDigit = 0;
        countSymbol= 0;
        countUpper = 0;
        countLower = 0;

        for (int i = 0; i < passChar.length; i++){
        digit = (int)passChar[i] ;
        symbol = (int)passChar[i];
        upper  = (int)passChar[i];
        lower  = (int)passChar[i];

        if(digit >=48 && digit <= 57){
          countDigit = 1;
          System.out.println(countDigit);
        }
        else if(symbol >=32 && symbol <= 47 || symbol >=58 && symbol <= 64 ||
                symbol >=91 && symbol <= 96 || symbol >=123 && symbol <= 126){
          countSymbol = 1;
          if (countSymbol == 1){

            System.out.println("hello");
          }
        }
        else if( upper >=65 &&  upper <= 90){
          countUpper = 1;
        }
        else {
          countLower = 1;
        }

 }
         //this is where i'm running into error

        if(passChar.length  >= 8  && countDigit == 1 && countSymbol && countUpper == 1 ||
          passChar.length >= 8 && countDigit == 1 && countSymbol && countLower == 1 ||
          passChar.length >= 8 && countDigit == 1 && countLower  && countUpper == 1 ||
          passChar.length >= 8 && countSymbol == 1 && countLower && countUpper == 1){

       System.out.println("Password Qualfies!");
       passwordScore = passwordScore + 10;

      }
      else {

       System.out.println("Password Doesn't Qualify!");


      }


  }

2 个答案:

答案 0 :(得分:3)

在java中,if语句中的参数应该是boolean

我发现您只是为countSymbol检查,而不是根据您的要求countSymbol == 0countSymbol > 0

if(passChar.length  >= 8  && countDigit == 1 && countSymbol==0 && countUpper == 1 ||
                                                     ^ change this

在其他Or条件中进行类似的更改

答案 1 :(得分:0)

如果按countSymbol && countUpper == 1,您试图说countSymbol countUpper应该等于1,那么&&的工作方式不是countSymbol == 1 && countUpper == 1 。您必须完整地写出两个条件:countLower && countUpper == 1。与node_modules相同。

有趣的事实:如果您使用COBOL编写,那么类似的符号会起作用:)