为啥这是一个遥不可及的声明? [关闭]

Posted

技术标签:

【中文标题】为啥这是一个遥不可及的声明? [关闭]【英文标题】:Why is this an unreachable statement? [closed]为什么这是一个遥不可及的声明? [关闭] 【发布时间】:2013-01-23 23:17:29 【问题描述】:

在此代码的底部,我收到“无法访问的语句”错误。我已经尝试了一些事情,但无法弄清楚为什么会这样。错误在代码的底部(我已经用 // 注释了错误所在的位置)请帮助我指出正确的方向,我被难住了!

/**
* Describes a certain model.
* 
* @author (Joshua Baker) 
* @version (1.0)
*/
public class Model

public static final int IN_PER_FOOT = 12;
public static final int BASE_RATE = 60;
public static final int TALL_INCHES = 67;
public static final double THIN_POUNDS = 140.0;
public static final int TALL_THIN_BONUS = 5;
public static final int TRAVEL_BONUS = 4;
public static final int SMOKER_DEDUCTION = 10;

private String firstName;
private String lastName;
private int heightInInches;
private double weightInPounds;
private boolean travel;
private boolean smokes;
private String newHeight;
private int perHourRate;

/**
 * Default constructor
 */
public Model()

    setFirstName ("");
    setLastName  ("");
    setHeightInInches  (0);
    setWeightInPounds  (0.0);
    setTravel  (false);
    setSmokes  (false);


/**
 * 
 */
public Model (String whatIsFirstName, String whatIsLastName, int whatIsHeight, double whatIsWeight,
boolean canTravel, boolean smoker)

    setFirstName  (whatIsFirstName);
    setLastName  (whatIsLastName);
    setHeightInInches  (whatIsHeight);
    setWeightInPounds  (whatIsWeight);
    setTravel  (canTravel);
    setSmokes  (smoker);


/**
 *@return first name
 */
public String getFirstName()

    return firstName;


/**
 *@return last name
 */
public String getLastName()

    return lastName;


/**
 *@return height in inches
 */
public int getHeightInInches()

    return heightInInches;


/**
 *@return the converted height 
 */
public String getNewHeight()

    return newHeight;


/**
 *@return weight in pounds
 */
public double getWeightInPounds()

    return weightInPounds;


/**
 *@return models pay per hour rate
 */
public int getPerHourRate()

    return perHourRate;



/**
 *@return travel
 */
public boolean getTravel()

    return travel;


/**
 *@return smokes
 */
public boolean getSmokes()

    return smokes;


/**
 * models first name
 */
public void setFirstName(String whatIsFirstName)

    firstName = whatIsFirstName;


 /**
 * models last name
 */
public void setLastName(String whatIsLastName)

    lastName = whatIsLastName;


 /**
 * models height in inches
 */
public void setHeightInInches(int whatIsHeight)

    if (whatIsHeight >0)
    heightInInches = whatIsHeight;
    



 /**
 * models weight in pounds
 */
public void setWeightInPounds(double whatIsWeight)

    if (whatIsWeight >0)
    weightInPounds = whatIsWeight;
    


 /**
 * can model travel
 */
public void setTravel(boolean canTravel)

    travel = canTravel;


 /**
 * does model smoke
 */
public void setSmokes(boolean smoker)

    smokes = smoker;


/**
 * Converts to feet and inches
 */
public String convertheightToFeetInches() 

int leftOver = (heightInInches %= IN_PER_FOOT);
int newHeight = (heightInInches % IN_PER_FOOT);
return newHeight + "Foot" + leftOver + "Inches";


/**
 * 
 */
public int calculatePayPerHour()
    if (heightInInches  >= TALL_INCHES && (weightInPounds <= THIN_POUNDS)) 
        perHourRate = BASE_RATE + TALL_THIN_BONUS;
        return perHourRate;
    
    else
    
        perHourRate = BASE_RATE;
        return perHourRate;
    

    if (travel)            //unreachable statement


        perHourRate = BASE_RATE + TRAVEL_BONUS;   
        return perHourRate;
    
    else
    
        perHourRate = BASE_RATE;
        return perHourRate;

    

    if (smokes)              //unreachable statement
        perHourRate = BASE_RATE - SMOKER_DEDUCTION;
        return perHourRate;
    

    else 

    






/**
 * Displays details
 */
public  void displayInfo()

    System.out.print("Name : " + getFirstName() + " ");
    System.out.println(getLastName());
    System.out.println("Height : " + getNewHeight() + "inches");
    System.out.println("Weight : " + getWeightInPounds() + "pounds");
    System.out.print("Travel : " + getTravel() + " " );
    System.out.print("Smokes : " + getSmokes() );
    System.out.println("Hourly rate : " + getPerHourRate() );

【问题讨论】:

下次,请添加有问题的代码。要找到隐藏在单个方法中的单个注释,需要滚动大量代码。 【参考方案1】:

这是因为您的程序将从您的第一个 if block 或相应的 else 块返回:-

if (heightInInches  >= TALL_INCHES && (weightInPounds <= THIN_POUNDS)) 
    perHourRate = BASE_RATE + TALL_THIN_BONUS;
    return perHourRate;

else

    perHourRate = BASE_RATE;
    return perHourRate;

System.out.println("This will never get printed. And will show compiler error");

所以,两个return语句中的任何一个都会被执行。因此,任何进一步的代码都无法访问。


似乎您希望获得所有service rates 的累积总和以获得最终的perHourRate,为此,您可以从每个if-else 块中删除return statement。然后对于第一个块之后的所有if-else 块,不要将current price 分配给perHourRate,而是执行compound addition +=

此外,由于您正在处理实例字段 - perHourRate,因此您根本不需要返回它。您对perHourRate 所做的更改可以使用getPerHourRate() 获得。因此,将返回类型更改为void

您可以尝试将您的calculatePayPerHour 方法更新为以下方法:

public void calculatePayPerHour()
    if (heightInInches  >= TALL_INCHES && (weightInPounds <= THIN_POUNDS)) 
        perHourRate = BASE_RATE + TALL_THIN_BONUS;  // Initial assignment

     else 
        perHourRate = BASE_RATE;  // Initial assignment
    

    /** Rest of the assignment will be compound assignment, since you
        are now updating the `perHourRate` **/

    if (travel)        
        perHourRate += TRAVEL_BONUS;   
     // You don't need an else now. Since BASE_RATE is already added

    if (smokes)     
        perHourRate -= SMOKER_DEDUCTION;
    

【讨论】:

现在很有意义!谢谢你们的帮助:) @JoshuaBaker.. 不客气 :) @JoshuaBaker.. 您可以将答案标记为已接受。【参考方案2】:

在您的 calculatePayPerHour 方法中,您有一个 if/else 并且永远不会到达该语句,因为在这两种情况下您都返回一个结果:

if (heightInInches  >= TALL_INCHES && (weightInPounds <= THIN_POUNDS)) 
    perHourRate = BASE_RATE + TALL_THIN_BONUS;
    return perHourRate; // you return in the if

else

    perHourRate = BASE_RATE;
    return perHourRate; // you return in the else


... the execution will never reach here

【讨论】:

【参考方案3】:
if (heightInInches  >= TALL_INCHES && (weightInPounds <= THIN_POUNDS)) 
    perHourRate = BASE_RATE + TALL_THIN_BONUS;
    return perHourRate; // <------------

else

    perHourRate = BASE_RATE;
    return perHourRate;  // <------------

无论您的身高或体重如何,这两个返回中的一个都会触发,因此它之后的任何语句都不会被执行。这与下面的代码相同。

if (heightInInches  >= TALL_INCHES && (weightInPounds <= THIN_POUNDS)) 
    perHourRate = BASE_RATE + TALL_THIN_BONUS;

else

    perHourRate = BASE_RATE;


return perHourRate;  
//unreachable

【讨论】:

【参考方案4】:

在代码上方的 if/else 中,您已经有 2 个返回语句... 这意味着它永远不会到达下面的代码..

【讨论】:

【参考方案5】: 在您的方法public int calculatePayPerHour() 中,第一个 if else 语句在任何情况下都会返回一个值(在 if 和 else 块中)。 如果您试运行该程序,您将看到 控制流 将永远不会到达此块下方的语句,因此您的异常。

【讨论】:

以上是关于为啥这是一个遥不可及的声明? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

gfortran编译错误错误:[关闭]的不可分类声明

为啥搜索引擎会忽略符号? [关闭]

为啥这是 C++ 中的有效函数声明? [复制]

为啥需要创建http服务器? [关闭]

MFC为啥一关闭对话框就系统崩溃 弹出英语说某个地址不可写。。。?

为啥必须在 C 中向后读取指针声明? [关闭]