Java 布尔方法中缺少返回值
你好,我正在为学校做一个项目,我很难弄明白为什么我的程序总是告诉我我的所有方法都缺少一个返回语句 这是我的密码:Java 布尔方法中缺少返回值,java,boolean,return-value,Java,Boolean,Return Value,你好,我正在为学校做一个项目,我很难弄明白为什么我的程序总是告诉我我的所有方法都缺少一个返回语句 这是我的密码: public class Temperature { private int temperature; //constructors public int Test( int temperature ) { temperature = temperature; return temperature; } public int TempClass() {
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;
返回0;
}
//获取和设置方法
公共int getTemp()
{
返回温度;
}
公共void settmp(int-temp)
{
温度=温度;
}
//确定物质是否存在的方法
//会结冰或沸腾
公共静态布尔值(int-temp)
{
int-f=-173;
如果(乙基F=温度)
系统输出打印(“乙基将在该温度下沸腾”);
其他的
返回false;
}
公共布尔值isoxygen(int-temp)
{
int OxyF=-362;
如果(氧=温度)
系统输出打印(“氧气将在该温度下沸腾”);
其他的
返回false;
}
公共布尔值为水冻结(int-temp)
{
int H2OF=32;
如果(H2OF=温度)
系统输出打印(“水在该温度下会沸腾”);
其他的
返回false;
}
}
以下是问题所在:
if (EthylF <= temp)
{System.out.print("Ethyl will freeze at that temperature");}
else
return false;
你可以清楚地看到问题所在
在if
分支中添加return true
可以解决此问题。int-f=-173;
int EthylF = -173;
if (EthylF <= temp)
{System.out.print("Ethyl will freeze at that temperature");}
else
return false;
if(EthylF temp
。否则,您有一个打印语句,但没有返回语句
非无效方法中的每个可能的执行路径必须以
return
语句或throw
语句结尾。查看isXXXFreezing(int temp)
和isXXXBoiling(int temp)中的if-else语句
methods:if语句下面的部分不包含return语句,只有else语句下面的部分包含return语句
的正确代码是
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;
}
public静态布尔值(int-temp){
int-f=-173;
如果(乙基fpublic int Test(int temperature){temperature=temperature;return temperature;}
a)不是构造函数,而b)未将参数指定给对象的字段。您认为为什么会出现此错误?除了代码中的其他问题外,我相信您的=
操作都是向后的。对于其他布尔getter,反之亦然。
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;
}