为什么我在减少时不能在for循环中使用'less than'运算符(<)

编程入门 行业动态 更新时间:2024-10-17 05:33:26
本文介绍了为什么我在减少时不能在for循环中使用'less than'运算符(<)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想这真是个愚蠢的问题但是: 为什么 - > for(int i = 1000; i> = 1; i- = 2)System.out.println(i); (Cmd打印i) ---> for(int i = 1000; i< = 1; i- = 2)System.out.println(i); (没有错误,没有在屏幕上打印) 为什么当我输入< = cmd时没有反应并且没有显示递减迭代? 谢谢你的补充! 我的尝试: for(int i = 1000; i< = 1; i- = 2) for(int i = 1000; i< 1; i - = 2)

解决方案

for(int i = 1000; i< = 1; i- = 2) for (int i = 1000; i< 1; i- = 2)

看看你的码。你开始时i< 1是假的,所以它什么都不做 当你使用> =版本时,你不会立即中止。 您需要重新考虑不等式的顺序!

首先,研究这个执行for循环的流程 [ ^ ] 如果

, (int i = 1000; i <= 1; i- = 2)System.out.println(i);

1.初始化:i初始化为1000,即i = 1000,然后进入2; 2.条件:结果i< = 1,即1000< = 1,为False,退出循环,因此

System.out.println(i);

将永远不会被执行。

Hi, I guess it's really silly question but: why --> for (int i = 1000; i >=1; i-=2) System.out.println(i); (Cmd prints i) ---> for (int i = 1000; i <=1; i-=2) System.out.println(i); (no errors, no printing on the screen) Why when i input <= cmd doesn't react and don't display decreasing iteration ? Thank you in addvance! What I have tried: for (int i = 1000; i <=1; i-=2) for (int i = 1000; i <1; i-=2)

解决方案

for (int i = 1000; i <=1; i-=2) for (int i = 1000; i <1; i-=2)

Look at your code. You start off with i<1 being false so it does nothing When you use the >= version, you don't abort immediately. You need to rethink about the order for the inequality!

First, study this Flow of Execution of the for Loop[^] in the case of

for (int i = 1000; i <=1; i-=2) System.out.println(i);

1. Initialization: i is initialized to 1000, i.e. i=1000, then it proceeds to 2; 2. Condition: the outcome of i <= 1, i.e. 1000 <= 1, is False, exit the loop, as such

System.out.println(i);

will never be executed.

更多推荐

为什么我在减少时不能在for循环中使用'less than'运算符(&lt;)

本文发布于:2023-11-22 03:42:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615839.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:我在   能在   运算符   amp   lt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!