【问题标题】:Finding the correct return type in java在java中找到正确的返回类型
【发布时间】:2016-02-25 01:31:31
【问题描述】:

我在制定正确的退货程序时遇到了问题。我想将第一个参数指定的金额类型的当前总数增加第二个参数中指定的金额。我遇到了一些问题。请帮帮我。

public void newValue(char amountType, double amount)
{
  if (amount <=0) // if amount entered is invalid
    throw new IllegalArgumentException("The amount needs to be 0 or larger "+amountType+" "+amount);

  if (amountType !='T' || amountType !='D' || amountType !='E') // if letter for the amount type is invalid (T,D, or E)
  totalTicketSales += amount;
  totalMoneyDonated += amount;
  totalExpenses += amount;

  throw new IllegalArgumentException("That is an invalid letter value. "+amountType+" "+amount
            + "The data will be ignored"); 

    currentAmount = amountType + amount;  //this is where my issue is
}

所以这是最新的。我试图根据您的反馈对此进行一些更改。

public double newValue(char amountType, double amount)
{
    double currentAmount = amountType + amount; 

  if (amount <=0) // if amount entered is invalid
  //I get an error here on this exception
    throw new IllegalArgumentException("The amount needs to be 0 or larger "+amountType+" "+amount);


  if (amountType !='T' || amountType !='D' || amountType !='E') // if letter for the amount type is invalid (T,D, or E)
  {
      totalTicketSales += amount;
  totalMoneyDonated += amount;
  totalExpenses += amount;

  throw new IllegalArgumentException("That is an invalid letter value. "+amountType+" "+amount
            + "The data will be ignored"); 
  }
     return currentAmount;
}

【问题讨论】:

  • 构造多行时使用括号{ }if statements
  • 这对我的返回值问题有帮助吗?
  • 你的方法被声明为not返回任何东西(void),所以return的值是多余的。如果有的话,您需要澄清您想要返回的内容。
  • 好的。所以你做出了改变。问题是否持续存在?有新问题吗?

标签: java methods try-catch return-type


【解决方案1】:
  1. Java 中的 if 语法是这样的:

    if (Statement) {
        sentence1;
        sentence2;
        ...
    }
    

    如果你只有一个句子,那么你不需要括号,但如果你的 if 语句中有多行,你需要将它们括在括号中。因此,您需要将第二个 if 括在括号中:

    if (amountType !='T' || amountType !='D' || amountType !='E') // if letter for the amount type is invalid (T,D, or E)
    {    
        totalTicketSales += amount;
        totalMoneyDonated += amount;
        totalExpenses += amount;
    
        throw new IllegalArgumentException("That is an invalid letter value. " + amountType + " "+ amount + "The data will be ignored"); 
    }
    
  2. 此外,您正在向字符添加双精度。这是做不到的。

    currentAmount = amountType + amount;  // YOU CAN'T ADD A DOUBLE TO A CHAR
    
  3. 您在方法中返回“无”。 void 方法意味着你不会返回任何东西。如果要返回双变量,则需要将方法头更改为:

    public double newValue(char amountType, double amount){
        code;
        ...
    }
    

这是一个将金额添加到 initialValue 的代码示例。如果 opType 是 T、D 或 E,则它会引发异常(基于您发布的代码)。

public double addAmount (double initialValue, double amount, char opType) {
    if (amount <= 0)
        throw new IllegalArgumentException("Amount must be > 0.");

    if (opType == 'T' || opType == 'D' || opType == 'E')
        throw new IllegalArgumentException("Invalid opType operation.");

    return initialValue + amount; // Now you are returning a double + double = double
}

【讨论】:

  • 您错过了另外两个主要问题,即 (1) 测试(&amp;&amp; 而不是 ||),以及 (2) char+double) 返回void 方法(或者这两个问题本身)。解释一下这些,我会投赞成票。
  • 为什么是&amp;&amp; 而不是||?他正在测试 char 是 T 还是 D 还是 E 以抛出异常。
  • 最大的问题是他在char中添加了一个double变量,这会引发编译错误。
  • OP 的意图非常不清楚。通过猜测意图来提供答案是有风险的。有时你可以猜到 90% 的把握……在这种情况下,我会说在某些问题得到解答之前,任何猜测都是
  • currentAmount = amountType + amount; 我相信这对我的代码来说是个大问题。
猜你喜欢
  • 2021-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多