【问题标题】:How to return a string for a double method in java如何在java中为double方法返回一个字符串
【发布时间】:2017-07-04 21:03:09
【问题描述】:

我是 Java 新手,我必须编写一个 withdraw 方法来检查帐户中是否有足够的资金。
如果账户余额低于0,它只会打印出一条消息说Insufficient funds

我尝试了以下方法:

public double withdraw(double accountbalance) {
    if(accountbalance <= 0) {
        return "Insufficient funds";
    }
}

【问题讨论】:

  • 这个方法不应该减去什么吗?另外,print != return
  • 我个人会抛出一个异常InsufficientFundsException 但是我们真的需要知道这个方法的逻辑应该做什么以及如何被称为
  • going to go below zero 但零不低于零,您应该使用&lt; 而不是&lt;=
  • 与您的具体问题无关,但使用double 作为货币通常是一个糟糕的主意。

标签: java


【解决方案1】:

你需要返回一个没有双精度的字符串 并且必须在 if 之外。例如:

public String withdraw(double accountbalance)
   String str="";

   if (accountbalance <= 0)
   {
     str="Insufficient funds"; 
   }
   return str;
 }

【讨论】:

    【解决方案2】:

    根据方法名withdraw(...),我相信这里应该有减法,应该有accountbalancewithdrawAmount值不足应该是accountbalance&lt;withdrawAmount

    您需要将返回类型从double 修改为String

    public String withdraw(double accountbalance)
     {
         if(accountbalance <=0){
             return "Insufficient funds";
         }
         return "Suffcient";
     }
    

    或者,我建议返回double,如果没有足够的值,则返回0。否则返回您要求的金额

    【讨论】:

    • 这给了我以下错误:返回类型与 Account.withdraw(double) 不兼容
    • @Michael 您的问题也不会自行编译。你想returnString还是returndouble
    • @cricket_007 我需要它返回一个字符串
    • @Michael 如果要返回 String,则必须在方法签名中将返回类型更改为String
    【解决方案3】:

    我认为,负账户余额是一个例外,因此应该这样实施。

    public double withdraw(double amount) {
      if (accountBalance - amount < 0) {
        // throwing an exception ends the method
        // similar to a return statement
        throw new IllegalStateException("Insufficient funds");
      }
      // this is only executed,
      // if the above exception was not triggered
      this.accountBalance -= amount;
    }
    

    现在你可以这样称呼它:

    public String balance(double amount) {
      // to handle potentially failing scenarios use try-catch
      try {
        double newBalance = this.account.withDraw(amount)
        // return will only be executed,
        // if the above call does not yield an exception
        return String.format(
          "Successfully withdrawn %.2f, current balance %.2f", 
          amount,  
          newBalance
        );
      // to handle exceptions, you need to catch them
      // exceptions, you don't catch will be raised further up
      } catch (IllegalStateException e) {
        return String.format(
          "Cannot withdraw %.2f: %s", 
          e.getMessage()
        );
      }
    }
    

    String.format 是一个方便的实用程序,用于格式化Strings,而无需将它们连接起来。它使用占位符,这些占位符被格式String之后的变量替换为相应的顺序。

    %s 代表String

    %f 是一个占位符,用于浮点数。在上述情况下,我使用了%.2f,它将浮点数格式化为小数点后2位。

    有关异常处理的更多信息可以在official documentationone of the many tutorials 中找到关于该主题的信息。

    【讨论】:

      【解决方案4】:

      将返回类型更改为 String 而不是 Double

      public String withDraw(double accountbalance) {
          if(accountbalance<=0) {
              return "Insfufficient funds";
          }
          return "Money is there";
      }
      

      【讨论】:

      • 为什么给答案 -1 这是每个问题的正确答案
      • 我不知道谁反对这些答案。它们都是有效的。我给他们一个赞成票
      • 如果你从字面上理解这个问题,这些答案是正确的,但它们并没有解决考虑到用户是 Java 新手并且可能是一般编程的背后的问题。后面的问题是如何根据具体情况切换返回类型。如果资金不足,则only 应返回该字符串。否则需要另一种返回类型。
      猜你喜欢
      • 2014-04-30
      • 2015-03-15
      • 2021-11-18
      • 2012-07-04
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 2018-02-24
      • 2015-07-07
      相关资源
      最近更新 更多