简体   繁体   English

如何定位列表中的每个第n个元素,但仅在3个元素之后

[英]How can I target every n-th element in a list but only after 3

How can I write a formula for this expression: 如何为该表达式编写公式:

boolean showAd = (position == 3 || position == 11 || position == 19 || position == 27 || position == 35 || position == 43|| position == 51);
        if (showAd) {.....

I tried position % 8 ==0 but this doesnt produce the same sequence as above 我尝试position % 8 ==0但这不会产生与上述相同的序列

Replace 更换

position % 8 ==0

by 通过

position % 8 == 3

The line 线

boolean showAd = (position == 3 || position == 11 ||
    position == 19 || position == 27 || position == 35 
    || position == 43|| position == 51);

will be similar to 将类似于

boolean showAd = (position - 3) % 8 == 0 // Note the parentheses

// (3 - 3) = 0 % 8 = 0
// (11 - 3) = 8 % 8 = 0
// (19 - 3) = 16 % 8 = 0
// (27 - 3) = 24 % 8 = 0
// ...

But is not necessary to store the boolean value in a separate variable (unless you will use it later): 但是没有必要将布尔值存储在单独的变量中(除非稍后使用):

if ((position - 3) % 8 == 0) {
   ...
}

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

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