如何将数字减 1 到 0 时回到起点?

How to decrease number by 1 and when it hits 0 goes back to the starting point?

我有一个长度为 3 的数组,因此索引为 0、1 和 2。然后我有一个计数器 (int c) 和一个翻转限制 (int rollover = array.length) 和两个按钮听他们点击时会决定是前进还是后退。

prev.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if(c!=0) {
                c--;
                updateLabel(pokemons[c]);
            } else if(c==0) {
                c = rollover;
                updateLabel(pokemons[c]);
            }

        }

    });

    next.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            c = (c+1) % rollover; //increase c until the limit
            if(c==rollover) {
                updateLabel(pokemons[c]);
            }
            else if(c<rollover) {
                updateLabel(pokemons[c]);

            }


        }

    });

"next" 按钮有效,但我的 "prev" 按钮在您位于索引 0 时遇到错误,但当您再次单击它时再次起作用。基本上我有一个显示口袋妖怪图片的 JLabel,当我单击下一个或上一个时,它会调用 updateLabel 方法来更改 JLabel 并显示数组中的下一个或上一个口袋妖怪。关于如何修复上一个按钮的任何想法?

我觉得这里应该是c = rollover-1;

        if(c!=0) {
            c--;
            updateLabel(pokemons[c]);
        } else if(c==0) {
            c = rollover-1; // <-- here
            updateLabel(pokemons[c]);
        }