【问题标题】:Prevent an Added Variable from Exceeding Specified Number防止添加的变量超过指定数量
【发布时间】:2014-12-12 20:14:45
【问题描述】:

我正在努力使我的方法 payGourmet 和 payEconomical 余额在没有足够的钱时不会改变并且不会低于零。也使我的 loadMoney 方法不超过 150,但仍从 main 添加指定的数字。我究竟做错了什么?

Import java.util.Scanner;

public class LyyraCard {

private double balance;

public LyyraCard(double balanceAtStart) {
    this.balance = balanceAtStart;
}
public String toString() {
    return "The card has " + this.balance + " euros";
}
public void payEconomical() {
    if (this.balance > 0) {
        this.balance -= 2.5;
    }
}
public void payGourmet() {
    if (this.balance > 0) {
        this.balance -= 4.0;
    }
}

public void loadMoney(double amount) {
    if (this.balance < 150) {
        this.balance += amount;
    }

}

}

public class Main {

public static void main(String[] args) {
    // add here code that tests LyraCard. However before doing 77.6 remove the
    // other code 
    LyyraCard card = new LyyraCard(10);
    System.out.println(card);

    card.payEconomical();
    System.out.println(card);

    card.payGourmet();
    System.out.println(card);

    card.payGourmet();
    System.out.println(card);

    card.loadMoney(10);
    System.out.println(card);

    card.loadMoney(200);
    System.out.println(card);

}

}

【问题讨论】:

  • 您检查余额是否优于 0,然后减去 4(或 2.5)。例如,如果余额为 2 怎么办? loadMoney 也是如此。

标签: java increment


【解决方案1】:

当您检查余额是否大于 0,然后减去一个金额,您最终可能会出现负余额:

public void payEconomical() {
  if (this.balance > 0) {
    this.balance -= 2.5;
  }
}

如果 balance = 1 这将产生负余额 (-1.5)。

您需要检查余额是否等于或大于您要减去的金额

public void payEconomical() {
  if (this.balance >= 2.5) {
    this.balance -= 2.5;
  }
  else {
    // There isn't enough money
  }
}

payGourmet 也是如此:

if (this.balance >= 4.0) {
...

loadMoney 中,您需要检查当前余额加上添加的金额是否等于或小于 150:

if (this.balance + amount <= 150.0) {
  this.balance += amount;
}
else {
  // Amount too large.
}

【讨论】:

    【解决方案2】:

    要将值限制为最小值或最大值,请使用Math.min()Math.max()

    int valueA = -50;
    
    valueA = Math.max(valueA, 0); //valueA is now 0;
    
    int valueB = 200;
    
    valueB = Math.min(valueB, 150); //valueB is now 150;
    

    如果您想将其限制在 上下限,请同时使用这两种方法

    int valueC = -50;
    
    valueC = Math.min(Math.max(valueC, 0), 150); //valueC is now 0
    
    int valueD = 200;
    
    valueD = Math.min(Math.max(valueC, 0), 150); //valueD is now 150
    

    编辑:所以对于你的例子,使用

    public void loadMoney(double amount) {
        this.balance = Math.min(this.balance + amount, 150);
    }
    

    【讨论】:

    • 我不会在经济的计划中这样做。如果我在卡上存入 2000 美元,那么至少 1850 美元就会消失得无影无踪。最好拒绝转移。
    • 这正是 OP 所要求的。他希望他的平衡场限制在150,这就是如何做到的。据我所知,OPs 程序中不存在拒绝转移的功能。
    • OP 没有要求扔掉钱。如果当前余额和金额不超过 150,则 OP 要求添加指定数量(1、10、13、103 甚至 150)。...does not exceed 150 but still adds the specified number from the main. - 告诉我 OP 想要添加 10,然后添加 200(请参阅主功能)。这可以有不同的解释,但在一个经济的计划中,一个根本不会破坏金钱。
    • 是的,你说服了我。我将其解释为当您超过上限时将其设置为最大值,但在超过上限时“拒绝”交易确实更有意义,至少在这种情况下是这样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-14
    • 1970-01-01
    • 2020-04-15
    • 1970-01-01
    • 2017-01-04
    • 1970-01-01
    • 2018-03-04
    相关资源
    最近更新 更多