好吧所以我的程序编译并运行得非常好,但目的是在摄氏,fahernheit和Kelvin之间进行转换,当程序与客户端一起运行时,它显示正确的单位(F,C,K)但是给出了所有温度的值均为50。我该如何解决这个问题?
public class Temperature
{
private double value;
private String scale;
public Temperature() // default constructor
{
this.value = 0;
this.scale = "C";
}
public Temperature(double value, String scale)
{
this.value = value;
this.scale = scale;
}
public double getValue()
{
return this.value;
}
public String getScale()
{
return this.scale;
}
public double getCelsius()
{
if (scale.equalsIgnoreCase ("C"))
{
return this.value;
}
else if (scale.equalsIgnoreCase("F"))
{
double faren = ((this.value - 32) * (5/9));
return faren;
}
else
{
double kelvin = (this.value - 273.15);
return kelvin;
}
} // end getCelcius
public double getFaren()
{
if (scale.equalsIgnoreCase ("F"))
{
return this.value;
}
else if (scale.equalsIgnoreCase("C"))
{
double celsius = ((this.value * 1.8) + 32);
return celsius;
}
else
{
double kelvin = ((this.value * 1.8) - 459.67);
return kelvin;
}
} // ene getFaren
public double getKelvin()
{
if (scale.equalsIgnoreCase("K"))
{
return this.value;
}
else if (scale.equalsIgnoreCase("C"))
{
double celsius = (this.value + 273.15);
return celsius;
}
else
{
double faren = ((this.value + 459.67) * (5/9));
return faren;
}
} // end getKelvin
public void setTemperature(Temperature t)
{
this.value = t.value;
}
public void setType(double degree, String measure)
{
this.value = degree;
this.scale = measure;
}
public void setValue(double degree)
{
this.value = degree;
}
public void setScale(String measure)
{
this.scale = measure;
}
public double convertToCelsius()
{
this.scale = "C";
return this.value = getCelsius();
}
public double convertToFaren()
{
this.scale = "F";
return this.value = getFaren();
}
public double convertToKelvin()
{
this.scale = "K";
return this.value = getKelvin();
}
public Temperature clone()
{
return new Temperature(this.value, this.scale);
}
public boolean equals()
{
return this.value == this.value && this.scale == this.scale;
}
public String toString()
{
String temps = ("The temperature is " + this.value + "(" + this.scale + ")");
return temps;
}
}
答案 0 :(得分:6)
你应该改变
public double convertToKelvin() {
this.scale = "K";
return this.value = getKelvin();
}
到
public double convertToKelvin() {
this.value = getKelvin()
this.scale = "K";
return value;
}
其他尺度相同。
您首先设置scale
,因此对象会看到scale
相同且不进行转换。