如何制作有界整数?

时间:2018-05-29 20:17:37

标签: java integer

对于将来的程序,我需要创建一个有界整数类,意思是0-59(可用于时间问题)。

我无法做到"环绕"。例如,如果我添加int = 54,则为10,应为4

1 个答案:

答案 0 :(得分:3)

您可以使用#this is what I would like to be able to do d.scores[:, 1:5].translate() # And I don't want to build a kwarg in the translate method to handle it like d.scores.translate(indices=[1]) 运算符

modulo %

如果您需要课程,可以执行以下操作:

int modulo = 60;
int value = 24;
value = (value + 40) % modulo;
System.out.println(value);        //  4
value = (value + 50000) % modulo;
System.out.println(value);        // 34

使用:

class MyIntegerBounded {
    private int value;
    private int bound;

    public MyIntegerBounded(int value, int bound) {
        this.value = value;
        this.bound = bound;
    }

    int get() {
        return value;
    }

    void increment() {
        add(1);
    }

    void add(int toAdd) {
        value = (value + toAdd) % bound;
    }
}