从方法内的决策语句调用时 println 不会发生(在 Java 中)
我在项目过程中遇到了一个有点烦人的问题。我创建了这个示例类来描述我遇到的问题。
public class Test {
public static void Testing(){
for (int i = 0; i >= 5; i++) {
System.out.println(i);
}
System.out.println("hello world.");
}
public static void main(String[] args) {
Testing();
}
}
我的问题是这个程序的唯一输出只是“hello world”。
谁能解释为什么我的 for 循环中的 println 语句被完全忽略的原因?我在谷歌上搜索过,但很难在搜索中描述。
多谢!
I have come across a somewhat annoying problem during a project. I created this sample class to describe the issue which I am having.
public class Test {
public static void Testing(){
for (int i = 0; i >= 5; i++) {
System.out.println(i);
}
System.out.println("hello world.");
}
public static void main(String[] args) {
Testing();
}
}
My issue is that the only output from this program is simply "hello world."
Could anyone explain the reason why my println statement inside the for loop is being completely ignored? I have searched on Google but it is hard to describe in a search.
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
for 循环应为
for (int i = 0; i <= 5; i++)
The for loop should be
for (int i = 0; i <= 5; i++)
海伙计。问题是合乎逻辑的。仔细查看 for 循环 for (int i = 0; i >= 5; i++)
for 循环应该是
for (int i = 0; i <= 5; i++)
Hai Buddy. the problem is logical.look at the for loop closely for (int i = 0; i >= 5; i++)
The for loop should be
for (int i = 0; i <= 5; i++)
我认为问题是你的循环永远不会执行,因为你的条件是 I 至少是 5,但你从零开始。尝试将其更改为小于或等于 5,看看是否可以解决问题。
I think the problem is that you loop never executes since your condition is that I is at least 5, but you start it at zero. Try changing it to be less than or equal to five and see if that fixes it.
改变for循环
change the for loop
再读一遍:
i 默认为零,当 i 大于或等于 5 时,for 会迭代。
read again:
i defaults to zero, and the for iterates while i is more or equal to 5.
因为您的条件 (
i >= 5
) 永远不会成立,因为您将i
设置为 0。条件应为i <= 5
。Because your condition (
i >= 5
) never is true, since you seti
to 0. The condition should bei <= 5
.原因是你的 for 循环从未被执行。第一步 i = 0 i>=5 = false 因此 for 的主体永远不会被执行
The reason is that your for loop is never executed. On first step i = 0 i>=5 = false so the body of the for is never executed
当 main 方法调用你的方法时,它首先用 0 初始化 i 的值,然后执行条件 i>=5,看起来像 0 >= 5 ,它总是“假”。所以你内部的 print 语句永远不会被执行。
When main method invoke you method it first initialize the value of i with 0 then its go for the condition i>=5, Which looks like 0 >= 5 which always be 'false'.So you inner print statement never be execute.
for 循环永远不会执行,因为在开始时会检查 i 是否等于或大于 5(实际上不是,i=0),
然后循环终止并执行下一条语句。
The for loop never execute because the at the beginning i is checked to see if it equal or greater than 5 (which it is not, i=0)
then the loop terminates and the next statement is executed.