for loop without index declaration(没有索引声明的for循环)
问题描述
所以我在某个地方声明了一个变量并对其进行了初始化.现在稍后我需要在它仍然为正时使用它来循环,所以我需要减少它.对我来说,循环使用条件和减量需要 for 但我们缺少初始化的第一部分.但我不需要初始化任何东西.那么我该如何以一种好的方式来解决这个问题.
So I declare a variable some where and initialize it. Now later on I need to use it to loop while its still positive so I need to decrement it. To me looping using a condition and a decrement calls for a for but for it we are missing the first part the initialization. But I don't need to initialize anything. So how do I go about that in a nice way.
for (space = space; space > 0; space--)//my first way to do it but ide doesnt like it
第二种方式:
for (; space > 0; space--)//my friend recommended me this way but looks kind weird
是否有更多方法可以让我拥有一个只有条件和递增/递减的循环?
Are there more ways for me to have a loop with only condition and increment/decrement?
P.S 拼写检查不知道递减"是一个词.我很确定它是......
P.S spell check doesn't know that "decrement" is a word. I'm pretty sure it is...
推荐答案
另一种方法是:
Integer i = 10;
while(--i>0) {
System.out.println(i);
}
当 i 为 0 而条件为假时...所以.. 它将从 9 打印到 1(9 项)
When i is 0 while condition is false... so.. it will print from 9 to 1 (9 items)
Integer i = 10;
while(i-->0) {
System.out.println(i);
}
将从 9 打印到 0...(10 项);
Will print from 9 to 0... (10 items);
这篇关于没有索引声明的for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:没有索引声明的for循环


- Java包名称中单词分隔符的约定是什么? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01