无法使我的布尔值起作用

Cannot get my boolean values to work

所以我应该创建一个程序,将温度设置为摄氏度和华氏度。我在其中创建了一个 equals 方法。然后我要创建一个 driver 来运行它并使用 equals 方法测试温度以查看它们是否相等。我无法获得正确的布尔值,它只是不断吐出错误。在这里撞墙。代码如下:

public class 温度 { public 枚举比例{ CELSIUS, FARENHEIT};

public static final double DEFAULT_DEGREES = 0.0;

private double temperature;
private Scale scale;

public Temperature()
{
    this(Temperature.DEFAULT_DEGREES);
}//default

public Temperature(double newTemperature)
{
    this(newTemperature, Scale.CELSIUS);
}//ending bracket of constructor double

public Temperature(Scale newScale)
{
    this(0, newScale);
}//end of constructor scale

public Temperature(double newTemperature, Scale newScale)
{
    this.setTemperature(newTemperature);
    this.setScale(newScale);
}//end bracket of constructor degrees and scale

public Scale getScale()
{
    return this.scale;
}//end of method getScale

public void setScale(Scale newScale)
{
    this.scale=newScale;
}//end of method setScale

public double getTemperature()
{
    return this.temperature;
}//ending bracket of metho getTemperature

public void setTemperature(double newTemperature)
{
    if(newTemperature < Temperature.DEFAULT_DEGREES)
    {
        this.temperature=Temperature.DEFAULT_DEGREES;
    }
    else
    {
        this.temperature = newTemperature;
    }//ending of if
}//ending bracket of method setTemperature

public double getTemperatureInFarenheit()
{
    double rv;

    if(this.getScale() == Scale.CELSIUS)
    {
        rv= this.getTemperature();
    }
    else
    {
        rv = ((this.getTemperature()* 1.8) + 32);
    }//end of if

    return rv;
}//end of bracket of method getweightinfarenheit


public double getTemperatureInCelsius()
{
    double rv;

    if(this.getScale() == Scale.FARENHEIT)
    {
        rv = this.getTemperature();
    }
    else
    {
        rv= ((this.getTemperature()- 32) * 0.5556);
    }

    return rv;
}//end of method gettemperatureincelsius

public boolean equals(Object otherObject)
{
    boolean rv = false;

    if(otherObject instanceof Temperature)
    {

        Temperature otherTemperature = (Temperature)otherObject;

        if((this.getTemperature()== otherTemperature.getTemperature())
        &&(this.getScale()== otherTemperature.getScale()))
        {
            rv = true;
        }//end of nested if
    }//end of if

    return rv;
}//end of bracket method equals

}//结束class

这是我的 driver:

public class 温度驱动器 {

public static void main(String[]args)
{


    Temperature w1 = new Temperature();
    w1.setTemperature(32);
    w1.setScale(Temperature.Scale.FARENHEIT);
    Temperature w2 = new Temperature();
    w2.setTemperature(0);
    w2.setScale(Temperature.Scale.CELSIUS);

    System.out.println(w1.equals(w2));

    Temperature w3 = new Temperature();
    w3.setTemperature(-40.0);
    w3.setScale(Temperature.Scale.FARENHEIT);
    Temperature w4 = new Temperature();
    w4.setTemperature(-40.0);
    w4.setScale(Temperature.Scale.CELSIUS);

    System.out.println(w3.equals(w4));

    Temperature w5 = new Temperature();
    w5.setTemperature(212);
    w5.setScale(Temperature.Scale.FARENHEIT);
    Temperature w6 = new Temperature();
    w6.setTemperature(100);
    w6.setScale(Temperature.Scale.CELSIUS);

    System.out.println(w5.equals(w6));




}//end of method main

}//结束class温度driver

所以我看到了几个问题。首先,在你的实施中 getTemperatureInCelsius()

if(this.getScale() == Scale.FARENHEIT)

我觉得应该是

if(this.getScale() == Scale.CELSIUS)

对于 getTemperatureInFarenheit 的实施,反之亦然。

一旦这些问题得到解决,我认为 equals 的主要问题是它坚持要相同的比例。你在哪里

if((this.getTemperature()== otherTemperature.getTemperature())
    &&(this.getScale()== otherTemperature.getScale()))

应该是

if(this.getTemperatureInFarenheit()== otherTemperature.getTemperatureInFahrenheit())

我特意在华氏温度下进行比较,以避免在您的摄氏转换系数中四舍五入。这可能会使 equals 工作(一旦我提到的 getTemperatureInFarenheit 修复完成)。

我仍然认为说

这样的话会让我感觉更好
if(Math.abs(this.getTemperatureInFarenheit()-otherTemperature.getTemperatureInFahrenheit())<0.001)

比我上面建议的要好,如果上面的方法失败了,我会尝试。