我必须编写一个接受温度值的类的代码,将其转换为开尔文,然后确定它是否大于,等于,小于或等于另一个输入的临时值。对于每个布尔可能性,它必须说真或假。我无法将设定为t1的温度与布尔方法作为参数的温度t进行比较?有什么建议?另外,放弃我,我知道我不知道我在做什么,为什么我问这个问题。我上周开始学习Java,我知道我不擅长它吗?
public class Temperature {
public double degrees;
public static void main(String[] args) {
}
Temperature (){
degrees=0;
}
Temperature (double enteredtemp){
degrees = enteredtemp;
}
Temperature (double enteredtemp,char scale ){
Temperature t1 = new Temperature (enteredtemp, scale);
t1.set(enteredtemp,scale);
}
public void set (double enteredtemp, char scale){
if (scale == 'r'|| scale == 'R'){ degrees = (enteredtemp/(9/5));}
else if (scale == 'c'|| scale == 'C') {degrees = enteredtemp+273.15;}
else if (scale =='F'|| scale == 'f'){degrees = ((enteredtemp+459.67)*9/5);}
}
public double get(){
return degrees;
}
public double get(char scale){
if (scale == 'c'|| scale == 'C'){degrees = (degrees-273.15);}
else if (scale == 'r'||scale == 'R'){degrees = (degrees*(9/5));}
else if (scale == 'f'|| scale == 'F'){degrees = (degrees*(9/5)-459.67);}
return (degrees);
}
public boolean isLessThan(Temperature t){
if (t.get() < t1.get())
return true;
else {
return false;
}
}
public boolean isGreaterThan(Temperature t){
if (t.get() > t1.get()) {
return true;
}
else {
return false;
}
}
public boolean isEqual(Temperature t){
if ((Math.abs(t.get() - t1.get()))<=10E-12){
return true;
}
else {
return false;
}
}
public boolean isGreaterThanOrEqual(Temperature t){
if (t.get() >= t1.get()){
return true;
}
else {
return false;
}
}
public boolean isLessThanorEqual(Temperature t){
if (t.get() <= t1.get()){
return true;
}
else {
return false;
}
}
}
答案 0 :(得分:0)
在此表单的功能中
public boolean isGreaterThan(Temperature t){
if (t.get()>t1.get()) {return true;}
else {return false;}
}
您正在将t
与不存在的t1
进行比较。您希望将当前对象(this
)与t
(您已传递的参数)进行比较。
public boolean isGreaterThan(Temperature t){
if (this.get()>t.get())
return true;
else
return false;
}
另请注意,在带有两个参数的构造函数中,您根本没有设置对象属性:您正在创建新的t1
,设置其属性,然后丢弃它。请致电this.set
而不是t1.set
。
答案 1 :(得分:0)
您发布的代码无法编译,因为您在此使用t1
等未知变量:
public boolean isGreaterThan(Temperature t){ if (t.get()>t1.get()) {return true;} else {return false;} }
您想放弃t1.
。
get
使用名为get
的方法来改变类的字段,这是一种反直觉和不好的做法。所以不要这样:
public double get(char scale) { if (scale == 'c' || scale == 'C') { degrees = (degrees - 273.15); } else if (scale == 'r' || scale == 'R') { degrees = (degrees * (9 / 5)); } else if (scale == 'f' || scale == 'F') { degrees = (degrees * (9 / 5) - 459.67); } return (degrees); }
像这样改写:
public double get(char scale) {
if (scale == 'c' || scale == 'C') {
return degrees - 273.15;
} else if (scale == 'r' || scale == 'R') {
return degrees * 9 / 5;
} else if (scale == 'f' || scale == 'F') {
return degrees * 9 / 5 - 459.67;
}
return degrees;
}
我还删除了不必要的括号。
此外,您的大部分方法都可以大大简化,例如:
public boolean isLessThan(Temperature t) {
return get() < t.get();
}
public boolean isGreaterThan(Temperature t) {
return get() > t.get();
}
public boolean isEqual(Temperature t) {
return Math.abs(t.get() - get()) <= 10E-12;
}
public boolean isGreaterThanOrEqual(Temperature t) {
return get() >= t.get();
}
public boolean isLessThanOrEqual(Temperature t) {
return get() <= t.get();
}
(我知道你赞扬你的教授&#34;不喜欢&#34; switch
(而且他错了),但无论如何都要留下来供你参考。)
我还建议像这样重写set
:
public void set(double enteredtemp, char scale) {
switch (scale) {
case 'r':
case 'R':
degrees = (enteredtemp / (9 / 5));
break;
case 'c':
case 'C':
degrees = enteredtemp + 273.15;
break;
case 'f':
case 'F':
degrees = ((enteredtemp + 459.67) * 9 / 5);
break;
}
}