从方法内的决策语句调用时 println 不会发生(在 Java 中)

发布于 2024-10-22 17:31:46 字数 454 浏览 10 评论 0原文

我在项目过程中遇到了一个有点烦人的问题。我创建了这个示例类来描述我遇到的问题。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

静若繁花 2024-10-29 17:31:46

for 循环应为

for (int i = 0; i <= 5; i++)

The for loop should be

for (int i = 0; i <= 5; i++)

表情可笑 2024-10-29 17:31:46

海伙计。问题是合乎逻辑的。仔细查看 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++)

尐籹人 2024-10-29 17:31:46

我认为问题是你的循环永远不会执行,因为你的条件是 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.

冷月断魂刀 2024-10-29 17:31:46

改变for循环

for(int i = 0; i <= 5; i++)

change the for loop

for(int i = 0; i <= 5; i++)
思念满溢 2024-10-29 17:31:46

再读一遍:

for (int i = 0; i >= 5; i++)

i 默认为零,当 i 大于或等于 5 时,for 会迭代。

read again:

for (int i = 0; i >= 5; i++)

i defaults to zero, and the for iterates while i is more or equal to 5.

昔日梦未散 2024-10-29 17:31:46

因为您的条件 (i >= 5) 永远不会成立,因为您将 i 设置为 0。条件应为 i <= 5

Because your condition (i >= 5) never is true, since you set i to 0. The condition should be i <= 5.

不奢求什么 2024-10-29 17:31:46
 for (int i = 0; i <= 5; i++) //You have put > sign it should be < sign
    {
        System.out.println(i);
    }
 for (int i = 0; i <= 5; i++) //You have put > sign it should be < sign
    {
        System.out.println(i);
    }
污味仙女 2024-10-29 17:31:46

原因是你的 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

莳間冲淡了誓言ζ 2024-10-29 17:31:46

当 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.

养猫人 2024-10-29 17:31:46

for 循环永远不会执行,因为在开始时会检查 i 是否等于或大于 5(实际上不是,i=0),

 for (int i = 0; i >= 5; i++)

然后循环终止并执行下一条语句。

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)

 for (int i = 0; i >= 5; i++)

then the loop terminates and the next statement is executed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文