gpt4 book ai didi

java - For循环空白条件

转载 作者:搜寻专家 更新时间:2023-10-31 19:33:21 24 4
gpt4 key购买 nike

for循环中的条件留空,代码编译运行。

 for(int i=0; ; i++)
System.out.print(i); //this code does not execute
//code after this does not execute

但是,我不明白如何以及为什么这是可能的。

最佳答案

只需更改下面的行,尽管这不是问题。

System.out.print(i);  //this code does not seems execute by checking o/p on console but in reals it works as well.

System.out.println(i);  //this code works and you will be able to see o/p on console.

System.out.print(i+" "); // this will show you some momentary action on Eclipse console.

在我看来,这是一些 Eclipse IDE 控制台打印问题。对于您在问题中提到的第一个版本,我看不到任何输出。由于 print() 不断在同一行上打印,我们可能看不到它。

但是,如果您在 Debug模式下运行您的代码并在上面的行上放置一个断点。断点将命中,您也可以看到正在打印的输出。

但是对于第二个版本,我可以看到它打印了从 0,1 开始的所有数字...

这是一个 similar discussion由@PakkuDon 分享

关于java - For循环空白条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23029007/

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