我正在为我的Java类简介项目,我们必须格式化UML,
+adjustQuantity(adjustingQuantity:int):void // Adjusts the book stored quantity by the given amount. The final
// must be >= 0
我已经有了添加已应用调整间隔的代码,
public void adjustQuantity(int adjustingQuantity)
{
int iAdjustingQuantity;
int iQuantity= this.quantity;
int iNewQuantity = (this.quantity + iAdjustingQuantity);
if(iNewQuantity <=0)
}
我遇到的问题是使值停止在0。我只需要执行if语句,说“如果小于0,则返回0”,但是它不返回任何内容,所以我不能这样做...所以我的问题如何使它保持积极而不是消极?
也许这个?
public void adjustQuantity(int adjustingQuantity) {
int iNewQuantity = this.quantity + adjustingQuantity;
if (iNewQuantity >= 0)
this.quantity = iNewQuantity
else
this.quantity = 0;
}
使用以上方法,您可以确保仅在新数量为零或正数时调整数量,否则我们将分配为零。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句