【发布时间】: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