2012-04-29 39 views
0
public class Driver { 

public static void main(String[] args) { 


    int length=0; 
    int MaxNumber=100; 
    StringBuilder password = new StringBuilder(); 





    do { 
     if (PasswordGenerator.matchLength (length)) { 
      break; 
     } 
     length++; // length is randomly picked 
    } while (length < MaxNumber); // or <100 
    System.out.println("The length of the character string is " + length); 


    int index = 0; 
    char f = 0; 


    for (char d = 0; d < 127; d++) { 
     if(PasswordGenerator.matchCharAt(d, index)) { 
      password.append(d); 
      System.out.println("Hey! Char at " + index + " is " + d); 
      d++; 
     } 
    } 



} 
} 

所以,我已經找到了如何找到我通過循環隨機生成的密碼的第一個字符,並想知道我怎麼能提供它通過使用該循環或找到密碼的其他字符另一個。字符遵循循環多長時間,這也是隨機生成的。到PasswordGenerator類的文檔是在這裏。(http://www.technology.heartland.edu/faculty/todds/csci130/assignments/A5_password/doc/index.html)循環通過生成的字符

+0

那'PasswordGenerator'的事情是非常不好的,它應該能夠至少給你的密碼長度不強迫你遍歷整個事情。 – debracey

回答

0

變化index的價值你試圖找到的角色的位置。如果你想找到所有的人則...

for(int j=0; j < length; j++) 
{ 
     for (char d = 0; d < 127; d++) 
     { 
     if(PasswordGenerator.matchCharAt(d, j)) 
     { 
      password.append(d); 
      System.out.println("Hey! Char at " + index + " is " + d); 

      // No sense in incrementing d and going through all of them 
      // You found it already, break this loop 
      break; 
     } 
    } 
} 
+0

我可以將這些字符追加到一行嗎? –

+0

您正在將字符附加到密碼「StringBuilder」。當它結束時,''password'變量將包含整個事物。然後您可以打印。如果你想在'password'中的每個字符之間使用換行符,使用'.appendLine()'。提問前請先閱讀API。 – debracey