【发布时间】:2016-06-02 07:16:57
【问题描述】:
这是我正在学习的入门编程课程。我创建了一个实例方法来将newValue 添加到总数中。
它在方法中有两个参数:
(识别金额类型和金额的字母)
我在第一个参数上成功了。
第二个是让我挣扎。我假设我们有一个 if 语句。我做到了,所以有金额类型,然后我有三个要使用的字母可以是真的。我设置了if(amountType == false),编译器说它是“无法访问的语句”。
if 语句的标准是“如果金额的字母无效(即不是 T、D 或 E),则抛出 IllegalArgumentException,并将消息返回给用户。
public double newValue(boolean amountType, double amount)
{
boolean T = amountType;
boolean D = amountType;
boolean E = amountType;
if (amount < 0)
{
throw new IllegalArgumentException("The amount needs to be 0 or larger");
}
return amount;
if(amountType == false)
// if not D, E, T.....then exception
{
throw new IllegalArgumentException("That is an invalid letter value. "
+ "The data will be ignored");
}
else
{
}
}
任何帮助将不胜感激。
【问题讨论】:
-
return amount;将控制流移动到您的方法之外,因此没有必要在其后添加任何代码,因为它不会被执行(这将是死代码)。 -
拥有
returned 后您将无法拥有代码,因为它永远不会被调用。如果您想继续,请不要返回。
标签: java if-statement unreachable-statement