2013-11-03 148 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语句的每可能的路径。