gpt4 book ai didi

java - 字符串索引错误

转载 作者:行者123 更新时间:2023-12-02 07:49:46 25 4
gpt4 key购买 nike

我不断收到错误消息,指出我的字符串索引超出了 String.charAt、PasswordGenerator.matchCharAt 和 Driver.main 的范围。我不知道这到底是什么意思。另外,我的角色不会附加到我已经实例化的 stringbuilder 类中的一行。我想知道这是否是由字符串索引错误引起的,或者是否是我的错。

public class Driver {

public static void main(String[] args) {
int length=0;
int MaxNumber=100;
StringBuilder password = new StringBuilder();

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

int index = 0;
char f = 0;

for (int d = 0; d < 127 || ; d++) {
if (PasswordGenerator.matchCharAt(f, index))
d = (char) index;
char aChar = (char)d;
password.append(aChar);
System.out.println("Password is: " + aChar);
index++;

}
}
}

最佳答案

您收到错误是因为 idx 将在 0 到 127 之间变化。PasswordGenerator 中的密码可能没有那么长。例如,在询问索引 57 处是否有匹配项之前,必须询问 57 是否小于密码长度。

所以你的任务是猜测生成器保存的密码?那么你应该这样做:

Get to know the length of the password.
For each index from 0 upto but excluding the length:
Guess the character at that index.

关于java - 字符串索引错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10359549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com