无论如何,如果运行一定次数,还要做一个其他的?

时间:2013-02-21 13:23:34

标签: java if-statement statements

e.g

else if (currentImage == nekoPics[4]) 
currentImage = nekoPics[5];

需要这个运行两次,所以我的猫动画会刮到两次,然后转移到其他地方,这可能是最糟糕的方式,但我很感兴趣,如果你可以有一个其他如果没有复制粘贴执行两次

else if (currentImage == nekoPics[5]) 
currentImage = nekoPics[4];

并且更改索引会导致只是循环而且他会一直刮擦。

感谢任何帮助

编辑:整个代码

private void scratch(){
    for (int i = xPos; i <getWidth()/2; i+=0){
            xPos = getWidth()/2;
            // swap images

            if (currentImage == nekoPics[0]) 
                currentImage = nekoPics[1];
            else if (currentImage == nekoPics[1]) 
                    currentImage = nekoPics[2];
            else if (currentImage == nekoPics[2]) 
                    currentImage = nekoPics[4];
            else if (currentImage == nekoPics[4]) 
                currentImage = nekoPics[5];
            else 
                i+=10;

            repaint();
            pause(150)

2 个答案:

答案 0 :(得分:2)

整个if-else逻辑是多余的。按照您应该显示的顺序将图像放入有序集合(数组,列表)中,如此

 Pictures[] seq = new Pictures[] { pic1, pic2, pic3, pic3, pi2, pic4, pic5 };

然后浏览集合,或维护一个指向下一张图片的索引。

答案 1 :(得分:0)

我认为你正在移动一系列图像,所以你可以这样做:

else if (currentImage == nekoPics[3]) {
    currentImage = nekoPics[4];
    countdown = 5;
} else if (currentImage == nekoPics[4]) {
    currentImage = nekoPics[5];
} else if (currentImage == nekoPics[5]) {
    if (countdown-- > 0)
        currentImage = nekoPics[4];
    else
        currentImage = nekoPics[6];
} else if ...

换句话说,当从3转换为4时,根据您想要重复4,5序列的频率设置倒计时变量。

然后根据倒计时从5转换为46