import java.util.Random;
public class PasswordRandomizer {
// Define the variables
private Random random = new Random();
private int passwordLength;
private String password = "";
public PasswordRandomizer(int length) {
// Initialize the variable
this.passwordLength = length;
}
public String createPassword() {
// write code that returns a randomized password
for(int i = 0; i < this.passwordLength; i++){
int j = random.nextInt();
char symbol = "abcdefghijklmnopqrstuvwxyz".charAt(j);
this.password = this.password + symbol;
}
return this.password;
}
}
我如何添加字符到一个字符串,我试过,但我得到这个错误:字符添加到字符串
"Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -414383904".
如果i = 5“abcde”,我不会打印出来,我发现一个错误,我没有设置随机数的限制,这给出了一些错误,但它仍然无法正常工作。 – UkoM 2014-10-30 17:30:29