我刚刚开始使用Java编程,我正在尝试打印一串文本,一个接一个的字符,延迟.这是我到目前为止所拥有的:
public class SlowPrintHello {
public static void main(String[] args) throws InterruptedException {
// Get message, convert to char array
String message = "Hello, World!";
char[] chars = message.toCharArray();
// Print a char from the array, then sleep for 1/10 second
for (int i = 0; i == chars.length; i++) {
System.out.print(chars[i]);
Thread.sleep(100);
}
// Repeat for all chars
}
}
Run Code Online (Sandbox Code Playgroud)
当我在Eclipse中运行时,控制台说没有输出就终止了.有谁知道发生了什么?
问题:
for (int i = 0; i == chars.length; i++)
Run Code Online (Sandbox Code Playgroud)
它应该是
for (int i = 0; i < chars.length; i++)
Run Code Online (Sandbox Code Playgroud)
它将永远返回false,从而打破你的for循环而不在其内部迭代
| 归档时间: |
|
| 查看次数: |
3585 次 |
| 最近记录: |