Java for 循环条件在使用 length() 与使用 int 时表现不同
Java for-loop condition behaves differently when using length() compared to using an int
我最近开始学习 Java 并且遇到了 for 循环的问题。这是一个代码片段:
for (int c = 0; c < inputString.nextLine().length(); c++) {
Random randomIndex = new Random();
currentString.append(alphabet[randomIndex.nextInt(26)]);
}
System.out.println(currentString.toString());
在上面的代码中,我尝试创建一个与用户输入的字符串长度相同的随机字符串。但是,循环只运行一次。例如,如果我输入一个长度为 4 的字符串,我将得到一个单字符的字符串。
如果我像这样硬编码循环条件
for (int c = 0; c < 4; c++) { // We should have a 4-character string.
Random randomIndex = new Random();
currentString.append(alphabet[randomIndex.nextInt(26)]);
}
System.out.println(currentString.toString());
我得到了我所期待的,一个 4 个字符的长字符串。
关于 length() 方法我是否遗漏了一些 属性?甚至 运行 System.out.println(inputString.nextLine().length())
也给出了输入字符串的预期长度。怎么回事?
问题是 nextLine() 被重复调用,每次都测试条件以查看是否在 for 循环中继续。要使字符串长度相同,您可能只想获取下一行的长度一次。所以最简单的方法是说:
int nextLineLen = inputString.nextLine().length();
for (int c = 0; c < nextLineLen; c++)
和以前一样。
function nextLine() 将 return 新的下一行所以当循环 运行 秒时间 (c=1): inputString.nextLine().length()
= 0.
请尝试:
int l = inputString.nextLine().length();
for (int c = 0; c < l; c++) { // We should have a 4-character string.
....
}
我想详细说明已接受的(正确的)答案。
for 语句的基础:
for ( [ForInit] ; [Expression] ; [ForUpdate] ) Statement
在 JLS 中被描述为:
14.14.1. The basic for Statement
The basic for statement executes some initialization code, then executes an Expression, a Statement, and some update code repeatedly until the value of the Expression is false.
即Expression
、Statement
和update代码多次执行,直到Expression
的值为false
.
你的情况 Expression
是
c < inputString.nextLine().length()
和(假设 inputString
是一个 Scanner
),每次计算该表达式时,它都会读取一个新的输入行,并将其长度与 c
的当前值进行比较。
这意味着如果您先输入一个 4 个字符 String
并按回车键,条件将为 true
,因为 c == 0
和 0 < 4
.
但是,下次计算表达式时,需要一个新的输入行。如果你只是按下 enter(我假设你按下了,给定你得到的输出),新的输入 String
将是一个空的 String
,所以条件将是 1 < 0
,这是false
,循环结束输出单个字符String
.
请注意,如果您没有按回车键,而是输入了另外 3 个至少 4 个字符的 String
,则循环会继续,并且会生成一个 4 个字符的随机 String
,如果您输入第 4 次迭代后的空 String
(或者实际上,长度 <= 4 的任何 String
)。
例如以下5行输入:
1234
1234
1234
1234
1234
产生长度为 4 的输出字符串
fwpl
当然,正确的解决方案是只读取一个输入行,并使用它的长度来确定循环的迭代次数:
Random randomIndex = new Random();
int length = inputString.nextLine().length();
for (int c = 0; c < length; c++) {
currentString.append(alphabet[randomIndex.nextInt(26)]);
}
请注意,我将 Random
实例的实例化移到了循环之外,因为没有理由为循环的每次迭代创建一个新实例。
System.out.println(currentString.toString());
我最近开始学习 Java 并且遇到了 for 循环的问题。这是一个代码片段:
for (int c = 0; c < inputString.nextLine().length(); c++) {
Random randomIndex = new Random();
currentString.append(alphabet[randomIndex.nextInt(26)]);
}
System.out.println(currentString.toString());
在上面的代码中,我尝试创建一个与用户输入的字符串长度相同的随机字符串。但是,循环只运行一次。例如,如果我输入一个长度为 4 的字符串,我将得到一个单字符的字符串。
如果我像这样硬编码循环条件
for (int c = 0; c < 4; c++) { // We should have a 4-character string.
Random randomIndex = new Random();
currentString.append(alphabet[randomIndex.nextInt(26)]);
}
System.out.println(currentString.toString());
我得到了我所期待的,一个 4 个字符的长字符串。
关于 length() 方法我是否遗漏了一些 属性?甚至 运行 System.out.println(inputString.nextLine().length())
也给出了输入字符串的预期长度。怎么回事?
问题是 nextLine() 被重复调用,每次都测试条件以查看是否在 for 循环中继续。要使字符串长度相同,您可能只想获取下一行的长度一次。所以最简单的方法是说:
int nextLineLen = inputString.nextLine().length();
for (int c = 0; c < nextLineLen; c++)
和以前一样。
function nextLine() 将 return 新的下一行所以当循环 运行 秒时间 (c=1): inputString.nextLine().length()
= 0.
请尝试:
int l = inputString.nextLine().length();
for (int c = 0; c < l; c++) { // We should have a 4-character string.
....
}
我想详细说明已接受的(正确的)答案。
for 语句的基础:
for ( [ForInit] ; [Expression] ; [ForUpdate] ) Statement
在 JLS 中被描述为:
14.14.1. The basic for Statement
The basic for statement executes some initialization code, then executes an Expression, a Statement, and some update code repeatedly until the value of the Expression is false.
即Expression
、Statement
和update代码多次执行,直到Expression
的值为false
.
你的情况 Expression
是
c < inputString.nextLine().length()
和(假设 inputString
是一个 Scanner
),每次计算该表达式时,它都会读取一个新的输入行,并将其长度与 c
的当前值进行比较。
这意味着如果您先输入一个 4 个字符 String
并按回车键,条件将为 true
,因为 c == 0
和 0 < 4
.
但是,下次计算表达式时,需要一个新的输入行。如果你只是按下 enter(我假设你按下了,给定你得到的输出),新的输入 String
将是一个空的 String
,所以条件将是 1 < 0
,这是false
,循环结束输出单个字符String
.
请注意,如果您没有按回车键,而是输入了另外 3 个至少 4 个字符的 String
,则循环会继续,并且会生成一个 4 个字符的随机 String
,如果您输入第 4 次迭代后的空 String
(或者实际上,长度 <= 4 的任何 String
)。
例如以下5行输入:
1234
1234
1234
1234
1234
产生长度为 4 的输出字符串
fwpl
当然,正确的解决方案是只读取一个输入行,并使用它的长度来确定循环的迭代次数:
Random randomIndex = new Random();
int length = inputString.nextLine().length();
for (int c = 0; c < length; c++) {
currentString.append(alphabet[randomIndex.nextInt(26)]);
}
请注意,我将 Random
实例的实例化移到了循环之外,因为没有理由为循环的每次迭代创建一个新实例。
System.out.println(currentString.toString());