对于公共无效setValue(int newcount)我该如何使其它程序发送的值用于设置newcount?另外我必须这样做“如果newcount为<为零或> maxValue,则什么也不做。”Java计数器问题
private int maxValue;
private int count;
/**
* Constructor for objects of class Counter
*/
public Counter(int maxValue)
{
maxValue = 0;
}
public void decrement()
{
if (count == maxValue)
{
count = maxValue;
}
else
{
--count;
}
}
public int getValue()
{
return maxValue;
}
public void increment()
{
if (count == maxValue)
{
count = 0;
}
else
{
++count;
}
}
public void setValue(int newcount)
{
}
public String toString()
{
return "Counter{" + "maxValue=" + maxValue + '}';
}
}
如果这是一个家庭作业问题,请标记为“作业” – 2010-09-28 20:04:36
在'decrement()'中您的意思是:if(count == 0)count = maxValue;'?另外,为什么不使用模运算?例如'count =(count + 1)%maxValue','count =(count + maxValue - 1)%maxValue'。 – 2010-09-28 20:21:40