gpt4 book ai didi

java - 使 adjustQuantity 方法停止在 0

转载 作者:行者123 更新时间:2023-11-30 09:15:41 24 4
gpt4 key购买 nike

我正在为我的 Java 类(class)介绍项目工作,我们必须格式化一个 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 - 使 adjustQuantity 方法停止在 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19798376/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com