2013-12-18 13 views
2
public double open() { 
    if (status.equalsIgnoreCase("Pending")) { 
     status = "Open"; 
     if (startPrice <= 5) { 
      return 0.2; 
     } else if (startPrice > 5 && startPrice <= 20) { 
      return 0.5; 
     } else if (startPrice > 20 && startPrice <= 100) { 
      return 1; 
     } else if (startPrice > 100 && startPrice <= 250) { 
      return 2.5; 
     } else if (startPrice > 250) { 
      return 5; 
     } 
    } else if (!status.equals("Pending")) { 
     return -1; 
    }   
} 

你能解釋爲什麼編譯器不斷要求缺少返回語句。我怎麼能解決這個問題爲什麼我的代碼不斷要求丟失返回語句

+0

status =「pending」 –

+1

java沒有足夠的智能來知道你的2 if語句是詳盡無遺的。你真的需要明確地告訴它。據java知道,if和else可能是假的,然後它不知道該怎麼做。它必須返回一個double,但你沒有告訴它該做什麼。 – Cruncher

回答

0

這是因爲,如果:

status.equals("Pending") 

回報true,你什麼都不返回。

您可以使用:

if (status.equalsIgnoreCase("Pending")) { 
     .... 
    } else { 
     return -1; 
    } 
1

這是抱怨,因爲你所有的return語句是if statements。只要刪除else if並在最後返回-1。

2

做這樣

public double open() { 
    if (status.equalsIgnoreCase("Pending")) { 
     status = "Open"; 
     if (startPrice <= 5) { 
      return 0.2; 
     } else if (startPrice > 5 && startPrice <= 20) { 
      return 0.5; 
     } else if (startPrice > 20 && startPrice <= 100) { 
      return 1; 
     } else if (startPrice > 100 && startPrice <= 250) { 
      return 2.5; 
     } else if (startPrice > 250) { 
      return 5; 
     } 
    } 
    return -1; 
}  
+0

@sᴜʀᴇsʜᴀᴛᴛᴀthanks ...... – Prabhakaran

+1

解釋爲什麼編譯器抱怨可能是有幫助的。 – Cruncher

+0

「你能解釋一下爲什麼編譯器不斷詢問丟失的返回語句嗎?」 – Troubleshoot

0

變化最後if else只是else提供一個備用的解決方案。您正在爲if else提供另一個if條件,因此如果此條件爲false,則不存在分支。

1

如果沒有

  1. status.equalsIgnoreCase( 「掛起」)
  2. !status.equals( 「待定」)

是真的嗎?

您還需要一個return語句。

3

編譯器不知道status.equalsIgnoreCase("Pending")!status.equals("Pending")涵蓋了所有可能性。事實上,我只是猜測,從檢查函數名稱。

你需要幫忙。

一個補丁修復 - 如果這是你的意圖 - 是寫

} else /*if (!status.equals("Pending"))*/ { 
    return -1; 
} 
1

Java是不夠聰明,以確定你保證返回。你需要明確地告訴它,通過對你的ifs進行其他處理,或者在結束時返回以捕獲任何流量。

在這種情況下,您的if語句也是多餘的。清理向上和工作版本將如下所示:

public double open() { 
    if (status.equalsIgnoreCase("Pending")) { 
     status = "Open"; 
     if (startPrice <= 5) { 
      return 0.2; 
     } else if (startPrice <= 20) { 
      return 0.5; 
     } else if (startPrice <= 100) { 
      return 1; 
     } else if (startPrice <= 250) { 
      return 2.5; 
     } else { 
      return 5; 
     } 
    } 
    return -1;   
} 

else if (startPrice > 5 && startPrice <= 20)當你有這條線,你已經知道了startPrice > 5。如果不是,那麼它會進入前一個if塊。

+0

+1的很好的解釋....我缺乏這一點。 – Prabhakaran

0

這一切都與return語句是本地的。如果所有的if語句都是false,那麼這個方法不會返回任何東西,因爲所有的返回語句都是if語句的本地語句。該程序無法知道該狀態否則會被掛起,因此編譯器會抱怨。

public double open() { 
    if (status.equalsIgnoreCase("Pending")) { 
     status = "Open"; 
     if (startPrice <= 5) { 
      return 0.2; 
     } else if (startPrice > 5 && startPrice <= 20) { 
      return 0.5; 
     } else if (startPrice > 20 && startPrice <= 100) { 
      return 1; 
     } else if (startPrice > 100 && startPrice <= 250) { 
      return 2.5; 
     } else if (startPrice > 250) { 
      return 5; 
     } 
    return -1; // no need for an else statement since if an one if statement returned true it wouldn't execute the rest of the code 
} 
相關問題