简体   繁体   English

循环中的Java循环

[英]Java loop inside a loop

What I want to do here is a loop inside a loop and I want to increment the inner loop by the value of the counter in the outer loop. 我想在这里做的是循环内部的循环,我想通过外循环中的计数器值递增内循环。

The error I get is "Not a statement", pointing at "b + s ) inside the inner for-loop 我得到的错误是“Not a statement”,指向内部for循环中的“b + s”

for( int s =1; s < 100; s++){
    if( 100 % s == 0){
        for( int b = 0; b < 100; b + s ){
            locker[b] = locker[b] * (-1);
        }
    }
}

Is my goal achievable at all? 我的目标是否可以实现?

Try changing this line: 尝试更改此行:

for( int b = 0; b < 100; b + s ){

Into: 成:

for( int b = 0; b < 100; b += s ){

This operator, also known as the addition assignment operator, will add s to b 's original value and store it back into b . 此运算符(也称为加法赋值运算符)将s添加到b的原始值并将其存储回b

Information is from Azure's comment . 信息来自Azure的评论

Where is b + s result stored? b + s结果存储在哪里?

What would be the point to let the third part's result of for loop, "volatilized"? 让第三部分的for循环结果“挥发”是什么意思? Maybe for side effects only..but would be too hidden.. not recommended. 也许只是副作用..但是太隐藏..不推荐。

Without a storing variable (pleonasm :)), your loop would end up with the same outcome at each step. 没有存储变量(pleonasm :)),你的循环在每一步都会得到相同的结果。

Thus, b += s would make more sense since b would store each new value, then usable in your loop content. 因此, b += s将更有意义,因为b将存储每个新值,然后可用于循环内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM