2012-04-29 53 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