java - 以循环方式打印值

标签 java

示例代码

int count=0;
while(true)
{ 
if(count>3)
System.out.print("value"+count);
count=count+1;
}

所需输出:

Value 0
Value 1
Value 2
Value 0
Value 1
Value 2

只要控制在 while 循环内流动,它就应该重复打印上面的值。如何更改上面的代码?

最佳答案

使用模数。

int count = 0;

while(true) {
   System.out.println("Value " + count); 
   count = ++count % 3;     
}

关于java - 以循环方式打印值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19112709/

相关文章:

java - 使用java中的方法限制进行安全发布

java - 调用远程方法时出错

java - hadoop 中 map 阶段之后的 reduce 阶段如何工作

java - 光规划器 : Planning entity and Planning variable at the same time?

java - HIbernate 空指针 bindManytoManyInverseFk

java - top -H 显示了几个没有出现在 jstack 输出中的线程 ID

java - 在Sqlserver中使用3des加密并使用java解密

java - 使用 SeleneseTestCase 运行 ANT 会导致 java.lang.NoClassDefFoundError

java - 速度指令也添加空格?

java - 共享 Netbeans 项目