如何使Java中的循环增加1以外的增量 [英] How to make for loops in Java increase by increments other than 1

查看:234
本文介绍了如何使Java中的循环增加1以外的增量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  for(j = 0; j <= 90; j ++){ } 

工作正常。但是当你有这样一个for循环:

$ p $ for(j = 0; j <= 90; j + 3){ }

不起作用。有人可以向我解释这个吗?

解决方案

这是因为 j + 3 不会改变 j 的值。你需要用 j = j + 3 j + = 3 替换它,这样<$ c对于(j = 0; j <= c c)而言,$ c> j 增加了3:

$ p $ 90; j + = 3){}


If you have a for loop like this:

for(j = 0; j<=90; j++){}

It works fine. But when you have a for loop like this:

for(j = 0; j<=90; j+3){}

it doesn't work. Could someone please explain this to me?

解决方案

That’s because j+3 doesn’t change the value of j. You need to replace that with j = j + 3 or j += 3 so that the value of j is increased by 3:

for (j = 0; j <= 90; j += 3) { }

这篇关于如何使Java中的循环增加1以外的增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆