对于输出0,-1,1,-2,2,-3,3的循环

时间:2017-05-26 17:32:58

标签: java for-loop

我正在制作一个Java游戏,我正在尝试使用for循环来获取块在x轴上来回交替生成。

4 个答案:

答案 0 :(得分:2)

做这样的事情:

spawn(0);

for(int i = 1; i <= 3; ++i)
{
    spawn(-i);
    spawn( i);
}

答案 1 :(得分:1)

Method createBlock(): //creates the blocks at desired x axis 

createBlock(0); //initial block placed at 0
int howMany= input
for (i=0; howMany;i++){
    createBlock(-i)
    createBlock(i)
}

答案 2 :(得分:0)

IntStream.iterate(0, i -> i >= 0 ? -i - 1 : -i). limit(100).forEach(i -> System.out.println(i));

答案 3 :(得分:0)

    System.out.print(0);
    for (int i = 1; i<=3; i++) {
        System.out.print(String.format(",%s,%s", -i, i));
    }