我正在开发一个 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;
}
通过上述内容,您可以保证仅当新数量为零或正数时才会调整数量,否则我们分配零。
关于java - 使 adjustmentQuantity 方法在 0 处停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19798376/