2014-02-16 61 views
0

我想讓我的密碼驗證器循環,直到給出正確的響應。它現在正在循環,但它不會在第一次迭代後重置布爾值,無論使用哪個密碼,第二次都會生成有效的響應。如何循環檢查每個值,或每次重置表?重置布爾值?

  boolean atLeast8 = false;     
     boolean oneLower = false;     
     boolean oneUpper = false;     
     boolean oneNumber = false;     
     boolean oneSpecial = false;     
     boolean noAnd = false;      
     boolean noEnd = false; 


      do 
     { 
      System.out.println("Enter your password. " + '\n'); 
      password = input.nextLine(); 
      System.out.println(); 




      for(int i=0; i<password.length(); i++)  //Checks for values througout the length of the string 

      { 
       char c = password.charAt(i); 
       if(password.length() >= 8) 
       atLeast8 = true; 

       if(Character.isLowerCase(c)) 
       oneLower = true; 

       if(Character.isUpperCase(c)) 
       oneUpper = true; 

       if(Character.isDigit(c)) 
       oneNumber = true; 

       if(c=='!' || c=='@' || c=='#' || c=='$' || c=='%' || c=='^' || c=='&' || c=='&' || c=='*') 
       oneSpecial = true; 

       if (password.indexOf("and") < 0) 
       noAnd = true; 

       if (password.indexOf("end") < 0) 
       noEnd = true; 

      } 
      if(atLeast8 && oneLower && oneUpper && oneNumber && oneSpecial && noAnd && noEnd) //Does the string contain any true values? 
       { 
        System.out.println("Valid."); 
       } 
      else 
       { 
        System.out.println("Invalid!");             //Does the string contain any false values? 
       } 

       if(!atLeast8) 
       { 
        System.out.println("Must be at least 8 characters long."); 


       } 
       if(!oneLower) 
       { 
        System.out.println("Must contain one lower case letter."); 

       } 
       if(!oneUpper) 
       { 
        System.out.println("Must contain one upper case letter."); 

       } 
       if(!oneNumber) 
       { 
        System.out.println("Must contain one numeric digit."); 

       } 
       if(!oneSpecial) 
       { 
        System.out.println("Must contain one special character."); 

       } 
       if(!noAnd) 
       { 
        System.out.println("Must not contain the word 'and'."); 

       } 
       if(!noEnd) 
       { 
        System.out.println("Must not contain the word 'end'."); 

       } 
     }while (atLeast8 == false || oneLower == false || oneUpper == false || oneNumber == false || oneSpecial == false || noAnd == false || noEnd == false); 
    } 

} 
+0

請用所使用的語言對此標記。 –

回答

0

你就不能把

atLeast8 = false;     
    oneLower = false;     
    oneUpper = false;     
    oneNumber = false;     
    oneSpecial = false;     
    noAnd = false;      
    noEnd = false; 

在循環的開始

或者說,是太(之間的「做{」和「對」的地方)簡單?

+0

我會嘗試一下,我不知道是否有適當的方法來解決這個問題或什麼... – user3313912

+0

這完全奏效!非常感謝,我希望我早日意識到這一點。 – user3313912