简体   繁体   English

Java中的if(boolean)不可达语句

[英]if (boolean)unreachable statement in Java

this is for a intro programming class i am taking. 这是我正在参加的一个介绍编程课程。 I have created a Instance method to add a newValue to the totals. 我创建了一个Instance方法来向总计添加newValue It has two parameters in the method: (a letter identifying the amount type, and amount) I was successful on the first parameter. 它在方法中有两个参数:(一个标识金额类型和数量的字母)我在第一个参数上成功了。 the second is making me struggle. 第二是让我挣扎。 I am suppose to us an if statement. 我想我们是if声明。 I made it so there is amount type, then i have three letters that are to be used that can be true. 我这样做了有数量类型,然后我有三个字母可供使用,这可能是真的。 I set the if(amountType == false) and the compiler says its a "unreachable statement". 我设置if(amountType == false) ,编译器说它是“无法访问的语句”。 The criteria for the if statement is "If the letter for the amount the is invalid (ie not T, D, or E), throw an IllegalArgumentException, and message back to user. 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
    {
    }
}    

Any help would be appreciated. 任何帮助,将不胜感激。

Your return statement is getting in the way: once executed, any code that falls afterwards will not be executed. 你的return语句会妨碍:一旦执行,之后落下的任何代码都不会被执行。 It needs to be the last instruction (not literally) to be executed in your method. 它必须是在您的方法中执行的最后一条指令(不是字面意思)。 You can do this instead: 你可以这样做:

public double newValue(boolean amountType, double amount) {
    boolean T = amountType;
    boolean D = amountType;
    boolean E = amountType;


    if (amount < 0) // Eliminate constraint 1
        throw new IllegalArgumentException("The amount needs to be 0 or larger");

    if (!amountType) // Eliminate constraint 2
        throw new IllegalArgumentException("That is an invalid letter value. "
                + "The data will be ignored");

    // Do your processing, now that you passed all tests

    return amount;
}

You have to put the return amount inside the first if block. 您必须将return amount放在第一个if块中。

The reason is that if the first if condition is true an exception will be thrown. 原因是如果第一个if条件为true ,则抛出异常。 And if it is evaluated as false , return amount will be executed. 如果将其评估为false ,则将执行return amount

In both cases, the second if block will never be executed 在这两种情况下,永远不会执行第二个if

Unreachable means that the line can never be reached in this method. 无法访问意味着在此方法中永远无法访问该行。 Because you add return statement without if statement, your second if statement can never be execute by program. 因为你添加了return语句而没有if语句,你的第二个if语句永远不会被程序执行。 So move return statement in your first if statement, and it will work. 所以在你的第一个if语句中移动return语句,它会起作用。

You have a return amount statement and it always executes and the code after it ie if statement is not reachable because the control always return from return amount. 你有一个return amount语句,它总是执行它后面的代码,即if语句是不可访问的,因为控件总是从返回金额返回。 One possible solution is first you have to check for amount type and then in the else part check for amount < 0 statement and the end return it. 一种可能的解决方案是首先必须检查金额类型,然后在else部分检查金额<0语句,结束返回它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM