2013-11-03 156 views
-2

您好,我對學校和IM有困難的大量試圖找出爲什麼我的程序總是告訴我的IM在我所有的方法缺少return語句的一個項目工作,缺少返回值

這裏是我的代碼:

public class Temperature { 
private int temperature; 

//constructors 
public int Test(int temperature) 
{ 
    temperature = temperature; 
    return temperature; 
} 
public int TempClass() 
{ 
    temperature = 0; 
    return 0; 
} 



// get and set methods 
public int getTemp() 
{ 
    return temperature; 
} 

public void setTemp(int temp) 
{ 
    temperature = temp; 
} 

//methods to determine if the substances 
// will freeze or boil 
public static boolean isEthylFreezing(int temp) 
{ 
    int EthylF = -173; 

    if (EthylF <= temp) 
    {System.out.print("Ethyl will freeze at that temperature");} 
    else 
    return false; 
} 

public boolean isEthylBoiling(int temp) 
{ 
    int EthylB = 172; 

    if (EthylB >= temp) 
    System.out.print("Ethyl will boil at that temperature"); 
    else 
    return false; 
} 

public boolean isOxygenFreezing(int temp) 
{ 
    int OxyF = -362; 

    if (OxyF <= temp) 
    System.out.print("Oxygen will freeze at that temperature"); 
    else 
    return false; 
} 

public boolean isOxygenBoiling(int temp) 
{ 
    int OxyB = -306; 

    if (OxyB >= temp) 
    System.out.print("Oxygen will boil at that temperature"); 
    else 
    return false; 
} 

public boolean isWaterFreezing(int temp) 
{ 
    int H2OF = 32; 

    if (H2OF <= temp) 
    System.out.print("Water will freeze at that temperature"); 
    else 
    return false; 
} 

public boolean isWaterBoiling(int temp) 
{ 
    int H2OB = 212; 

    if (H2OB >= temp) 
    System.out.print("Water will boil at that temperature"); 
    else 
    return false; 
} 
} 
+0

'public int Test(int temperature) { temperature = temperature; 返回溫度; }'a)不是構造函數,b)不會將參數賦值給對象的字段。 –

+0

你爲什麼認爲你得到這個錯誤? –

+0

除了您的代碼的其他問題,我相信您的「<=' and '> =」操作都是向後的。 –

回答

1

查看isXXXFreezing(int temp)isXXXBoiling(int temp)方法中的if-else語句:if語句下面的部分不包含return語句,只有else下面的部分有效。

isEthylFreezing(int temp)正確的代碼將

public static boolean isEthylFreezing(int temp) { 
    int EthylF = -173; 

    if (EthylF <= temp) 
    { 
    System.out.print("Ethyl will freeze at that temperature"); 
    return true; 
    } 
    else 
    return false; 
} 

另外在Test構造您所指定的變量temperature到自身。我想你想把函數參數temperature分配給一個Test的成員變量,它也被命名爲temperature?如果是這樣,請寫this.temperature = temperature;this引用當前的Test對象,並將確保您訪問成員變量。

4

這裏的問題是線:

if (EthylF <= temp) 
{System.out.print("Ethyl will freeze at that temperature");} 
else 
return false; 

編譯器認爲return false屬於與else分支,因此如果採取EthylF <= temp分支,該方法結束而不返回值。其餘的boolean獲得者也一樣。

正確縮進,並使用大括號會幫助你避免這樣的問題:當你看到格式化爲相同的代碼如下

if (EthylF <= temp) { 
    System.out.print("Ethyl will freeze at that temperature"); 
} else { 
    return false; 
} 

你看看到底哪裏出了問題。

if分支中加入return true就可以解決這個問題。

+0

反之亦然,爲其他布爾getter。 –

1
int EthylF = -173; 

if (EthylF <= temp) 
{System.out.print("Ethyl will freeze at that temperature");} 
else 
return false; 

如果是EthylF > temp,此方法只返回(false)。否則,你有一個打印,但沒有返回聲明。

一個非void方法內執行必須以return語句結束,或與throw語句的每可能的路徑。