2013-03-19 111 views
-1

編譯器指出語句在if語句的行中無法訪問。我不太熟悉Java。爲什麼說我的If語句無法訪問?

public double calculate() 
{ 
    total_usage_charge = getUsageCharge(); 
    total_charge = rate + total_usage_charge; 

    return total_charge; 

    if("A".equals(package_plan.toUpperCase()) && getUsageCharge() > 14.95) 
    { 
    sB = getUsageCharge() - 14.95; 
    System.out.println("You're spending more money than you should. If you switched to Plan B you would save:$" + sB); 
    } 
} 
+2

我刪除了幫助案例和其他混亂。請嘗試刪除不需要的代碼以幫助我們(和您!)專注於實際問題。 – 2013-03-19 08:07:15

+1

如果編譯器說某些語句不可訪問意味着在代碼執行過程中無法達到聲明。編譯器可以在編譯期間發現一些無法訪問的代碼(例如:在last語句之前返回,if(false){statement}等) 但要小心,因爲編譯器無法找到所有情況,因爲程序執行路徑取決於先前執行的所以你必須注意 – pedjaradenkovic 2013-03-19 08:13:02

回答

6

因爲你通過執行這個從方法返回:

return total_charge; 

所以下面的語句是永遠不會被執行。

+1

謝謝你對我的愚蠢問題如此耐心 – user2185503 2013-03-19 08:06:25

+1

即使我過去問我愚蠢的問題時,愚蠢的問題.. :) – RAS 2013-03-19 08:08:04

3

你只是if語句之前返回(return total_charge;)從你的方法。 return之後沒有任何代碼可以執行(除非相關finally塊,如果您的返回語句位於try...catch...finally)。

+1

這意味着很多,你回答了我很noobish問題 – user2185503 2013-03-19 08:06:41

2

您在調用if語句之前正在返回,因此它是不可緩解的。

方法返回到達到return語句時調用它的代碼。

+0

Ohhhhhh ....好的,謝謝。對不起。這可能是非常明顯的,但在Java中我真的很糟糕。 – user2185503 2013-03-19 08:05:59

+0

@ user2185503 return在其他語言中工作也一樣。 – 2013-03-19 08:08:57

1
return total_charge; 

您的方法返回在這一點上,後面的任何代碼將無法訪問。

-1
**if("A".equals(package_plan.toUpperCase()) && getUsageCharge() > 14.95) 
     { 
      sB = getUsageCharge() - 14.95; 
      System.out.println("You're spending more money than you should. If you switched to Plan B you would save:$" + sB); 
     } 
     else if("A".equals(package_plan.toUpperCase()) && getUsageCharge() > 19.95) 
     { 
      sC = getUsageCharge() - 19.95; 
      System.out.println("You're spending more money than you should. If you switched to Plan C you would save:$" + sC); 
     } 
     else if("B".equals(package_plan.toUpperCase()) && hours < 10) 
     { 
      sA = getUsageCharge() - 9.95; 
      System.out.println("You're spending more money than you should. If you switched to Plan A you would save:$" + sA); 
     } 
     else if("B".equals(package_plan.toUpperCase()) && getUsageCharge() > 19.95) 
     { 
      sC = getUsageCharge() - 19.95; 
      System.out.println("You're spending more money than you should. If you switched to Plan C you would save:$" + sC);** 
     }** 

你是否打算評論它,如果是的話。或者根據提供的代碼,這是錯誤和無法達到的聲明。

/* your code to comment */

+0

應該是**突出**我想。 – 2013-03-19 08:09:34

1

評論你的推杆上面IF return語句。所以當編譯器每次從這裏返回時都會發出這個語句,如果不能在任何時候執行,那麼刪除返回語句或將其放在下面,如果條件。

0
public double calculate() 
{ 
    total_usage_charge = getUsageCharge(); 
    total_charge = rate + total_usage_charge; 

    if("A".equals(package_plan.toUpperCase()) && getUsageCharge() > 14.95) 
    { 
    sB = getUsageCharge() - 14.95; 
    System.out.println("You're spending more money than you should. If you switched to Plan B you would save:$" + sB); 
    } 

    return total_charge; 
} 

就這樣;)

1

代碼不會的代碼返回statement.following塊是造成問題即

return total_charge; 

後執行那麼你將不得不刪除此行或將它在最後。