小编Bob*_*Bob的帖子

在java中翻转标志的计数器

好吧,所以我试图制作一个程序,我需要让计数器从-3开始,然后下降2,但每个其他数字必须是正面的:例如:

-3,5,-7,9,-11,13,-15,17,-19,......

任何输入?

我做了一个成功的计划,但我觉得这不是很有效.

    while ("expression")
    {
        if (j % 4 == -1) //checks if number should be negative
            j = Math.abs(j);

        if (j > 0) //makes counter a negative
            j = -j;

        j -= 2; //goes down by 2
    }
Run Code Online (Sandbox Code Playgroud)

java algorithm math performance counter

1
推荐指数
2
解决办法
301
查看次数

标签 统计

algorithm ×1

counter ×1

java ×1

math ×1

performance ×1