我有几个问题。我是java新手,很好奇如何创建模数计数器。刚开始我有一个Driver类和一个Counter类。
我的反击类是:
public class Counter {
private int count;
private int modulo;
public Counter () {
count = 0;
this.modulus= (count + 1)%8;
}
public int currentCount () {
return count;
}
public void incrementCount () {
count = count + 1;
}
public void reset () {
count = 0;
}
}
我添加了模数构造函数,但我对如何在我的驱动程序类中实现它感到困惑。任何输入都有帮助谢谢!
新计数器
public class Counter_01 {
private int count;
private int x;
public Counter_01 (int x) {
this.count = x;
this.x = x;
}
public void incrementCount () {
count = (count + 1) % x;
}
public int currentCount () {
return count;
}
public void reset () {
count = 0;
}
}
新司机
public class Driver {
public static void main(String[] args){
int count;
Counter myCounter = newCounter(x);
do {
if (myCounter.currentCount() <=10){
count = myCounter.incrementCount();
}
else {
myCounter.reset();
}
} while (count < 11);
}
}