为了记录,我的代码没有经过测试,并且需要永远完成。 第一个字母永远需要字母表中更远的字母。 提前感谢您的任何想法。
public class BruteForce {
public static void main (String[] args) {
System.out.println ("write your password (5 character only)");
String password = TextIO.getlnString ();
String word = "";
char letters[] = new char [5];
letters [0] = 'a';
letters [1] = 'a';
letters [2] = 'a'; //starts all the 5 letters with 'a'
letters [3] = 'a';
letters [4] = 'a';
while (word != password) {
//This seems like it would stop the program, but it's telling me that the variable(word) hasn't been initialized
for (letters [0] = 'a' ; letters [0] <= 'y' ; letters [0]++) {
word = new String (letters);
System.out.println (word);
for (letters [1] = 'a' ; letters [1] <= 'y' ; letters [1]++) {
word = new String (letters);
System.out.println (word);
for (letters [2] = 'a' ; letters [2] <= 'y' ; letters [2]++) {
word = new String (letters); //nested for loops
System.out.println (word);
for (letters [3] = 'a' ; letters [3] <= 'y' ; letters [3]++) {
word = new String (letters);
System.out.println (word);
for (letters [4] = 'a' ; letters [4] <= 'y' ; letters [4]++) {
word = new String (letters);
System.out.println (word);
}
}
}
}
}
}
}
}
答案 0 :(得分:0)
我建议你尝试类似的事情,
StringBuilder sb = new StringBuilder();
loop: for (char i = 'a'; i <= 'z'; i++) {
for (char j = 'a'; j <= 'z'; j++) {
for (char k = 'a'; k <= 'z'; k++) {
for (char l = 'a'; l <= 'z'; l++) {
for (char m = 'a'; m <= 'z'; m++) {
sb.append(i).append(j).append(k).append(l).append(m);
System.out.println(sb.toString());
if (sb.toString().equals(password)) {
break loop;
}
sb.setLength(0);
}
}
}
}
}