循环通过号码列表

时间:2014-10-16 05:37:29

标签: java modulo

如何在java中传播数字

1 = 1,2 = 2,3 = 3,4 = 1,5 = 2,6 = 3,7 = 1等

因此在一组有限的可能性中包含一组无限的数字?

2 个答案:

答案 0 :(得分:0)

public int modulo(int a, int b) {
    int r = a % b;
    return r==0?b:r;
}

assert(modulo(1,3) == 1)
assert(modulo(2,3) == 2)
assert(modulo(3,3) == 3)
assert(modulo(4,3) == 1)
assert(modulo(5,3) == 2)

答案 1 :(得分:0)

使用Guava 18:

import static com.google.common.collect.Iterables.cycle;
import static com.google.common.collect.Iterables.limit;
import static com.google.common.collect.Lists.newArrayList;

System.out.println(newArrayList(limit(cycle(1, 2, 3), 7)));
// [1, 2, 3, 1, 2, 3, 1]