使用数组中的单个数字循环打印

时间:2019-02-23 18:02:42

标签: java

我看过很多方法,但它们似乎对我试图做的事情没有作用。 我尝试过哈希集和for循环。

public static int count(){
        HashSet<Integer> excludedWorlds = new HashSet<>(Arrays.asList(300, 303, 304, 307));
        for (int i = 300; i < 308; i++) {
            if (excludedWorlds.contains(i)) continue;
            return i;
            // Do stuff
        }

        return -1;
    }

所以我放了

public int count =count();

然后我打印

count++

对于每个循环,但我的答案最终还是

301, then 302, then 303 then 304 then 305.

但我希望答案是 301, then 302, then 305, then 306, and loop back to 301重新开始。 我打开了其他任何方法来实现相同的目的。

1 个答案:

答案 0 :(得分:1)

我将使用一个数组,从索引值df.set_index(['Dest','Origin']).loc[top_5_route].reset_index() 开始,并在增加数组长度的模数的同时进行预递增。喜欢,

-1

(按要求)这将返回301-> 302-> 305-> 306,然后循环回到301。我测试了

private static int[] values = { 301, 302, 305, 306 };
private static int index = -1;

public static int count() {
    return values[++index % values.length];
}