Java函数以计算电荷差异?

huangapple go评论54阅读模式
英文:

Java function to calculate difference in charge?

问题

这个对象最大充电量为1000。当你先消耗100,然后再消耗1000时,第二次消耗应该返回900。我应该使用什么函数来实现返回900,因为不能消耗负数?

我尝试了以下代码:

public double drain(double minutes) {
    double drain = cameraPowerConsumption * minutes;
    batteryCharge = Math.max(batteryCharge - drain, 0);
    totalDrain += drain;
    return drain;

但它返回的是1000而不是900,已经消耗了100。我认为需要更改drain变量。不使用条件语句的情况下。

英文:

This object has a max charge of 1000. And when you drain 100, and then drain 1000, the second drain is supposed to return 900. What function to use that will allow me to return 900 because that is that max amount that can be drained as you can't drain a negative number?

I tried,

public double drain(double minutes) {
	    double drain = cameraPowerConsumption * minutes;
	    batteryCharge = Math.max(batteryCharge - drain, 0);
	    totalDrain += drain;
        return drain;

but it returns 1000 instead of 900, after already draining 100. I think the drain variable needs to be changed. Without conditionals if that matters.

答案1

得分: 0

要在减去较大的值时保留batteryCharge的值,可以使用三元运算符(来源),表示if/else。如果batteryCharge - drain小于0,则保留该值,否则保存减法值。可以使用以下方式代替:

batteryCharge = Math.max(batteryCharge - drain, 0);

尝试这个:

batteryCharge = batteryCharge - drain < 0 ? batteryCharge : batteryCharge - drain;

请注意,这些代码示例是用Java编写的。

英文:

To keep the batteryCharge value when subtracting a larger value, you can use the ternary operator (source) representing if/else. If batteryCharge - drain is less than 0 then keep that value, otherwise save the substraction value. Instead use this:

batteryCharge = Math.max(batteryCharge - drain, 0);

Try this:

batteryCharge = batteryCharge - drain &lt; 0 ? batteryCharge : batteryCharge - drain;

答案2

得分: 0

The amount drained cannot be greater than the battery's remaining charge:

double drain = Math.min(batteryCharge, cameraPowerConsumption * minutes);

After making that adjustment to your code, the rest is simple:

double drain = Math.min(batteryCharge, cameraPowerConsumption * minutes);
batteryCharge -= drain;
totalDrain += drain;
return drain;
英文:

The amount drained cannot be greater than the battery's remaining charge:

double drain = Math.min(batteryCharge, cameraPowerConsumption * minutes);

After making that adjustment to your code, the rest is simple:

double drain = Math.min(batteryCharge, cameraPowerConsumption * minutes);
batteryCharge -= drain;
totalDrain += drain;
return drain;

答案3

得分: 0

public double drain(double minutes) {
    double drain = cameraPowerConsumption * minutes;
    if (drain > batteryCharge) {
        totalDrain += batteryCharge;
        return batteryCharge;
    }
    totalDrain += drain;
    return (batteryCharge - drain);
}
英文:

Just simple

public double drain(double minutes) {
    double drain = cameraPowerConsumption * minutes;
    if (drain &gt; batteryCharge){
        totalDrain +=batteryCharge;
        return batteryCharge;
    }
    totalDrain +=drain;
    return (batteryCharge - drain);
}

huangapple
  • 本文由 发表于 2023年2月10日 06:51:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75405269.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定