英文:
conversion toCelsius() Method not working properly
问题
我写了一个方法,可以将温度从度转换为摄氏度,但当我在主方法中运行它时,得到了错误的输出。
理想情况下,212华氏度应该是摄氏100度;然而,我得到的输出却是-273.15。哎呀,有什么想法吗?
*编辑- 我第一次忘记加上break;,后来我注意到了。在插入后,我得到了0.0这个数字,以下是我最新的代码。
public class Temperature {
private double degree;
private char degreeUnit;
public Temperature(double degree, char degreeUnit) {
this.degree = degree;
this.degreeUnit = degreeUnit;
}
public Temperature toCelsius() {
switch (this.degreeUnit) {
case 'F':
this.degree = (this.degree - 32) * (5.0 / 9.0);
this.degreeUnit = 'C';
break;
case 'K':
this.degree = this.degree - 273.15;
this.degreeUnit = 'C';
break;
default:
this.degreeUnit = 'C';
}
return this;
}
}
public class TemperatureDemo {
public static void main(String[] args) {
Temperature temp5 = new Temperature(212, 'F');
System.out.println(temp5.toCelsius());
}
}
英文:
I wrote a method that converts degree units to Celsius, but when I run it in the main method, it gives the wrong input.
Ideally, 212 F gives 100 C; however, the output I got was -273.15. Oh gosh. Any ideas?
*edit- I missed the break; the first time so I got that. After the insertion, I got a number of 0.0, below are my most current code.
public class Temperature {
private double degree;
private char degreeUnit;
public Temperature(double degree, char degreeUnit) {
this.degree= degree;
this.degreeUnit= degreeUnit;
}
public Temperature toCelsius() {
switch(this.degreeUnit) {
case 'F':
this.degree=(this.degree-32)* (5/9);
this.degreeUnit= 'C';
break;
case 'K':
this.degree=this.degree-273.15;
this.degreeUnit= 'C';
break;
default:
this.degreeUnit= 'C';
}
return this;
}
}
public class TemperatureDemo
{
public static void main(String[] args)
{
Temperature temp5 = new Temperature(212, 'F');
System.out.println(temp5.toCelsius());
}
}
答案1
得分: 0
你需要在每个 case 后面加上 break
语句。否则,它们会直接进入下一个 case。
case 'F':
this.degree = (this.degree - 32) * (5.0 / 9);
this.degreeUnit = 'C';
break;
case 'K':
this.degree = this.degree - 273.15;
this.degreeUnit = 'C';
break;
default:
this.degreeUnit = 'C';
还有一些其他问题。
一个问题是 (5/9)。那是整数除法,结果会是 0。试试改成 (5.0 / 9)。
并且在你的温度类中重写 toString 方法,在指定对象时打印出数值。类似这样:
@Override
public String toString() {
return degree + " " + degreeUnit;
}
英文:
You need to put in break
statements after your cases. Otherwise, they just fall thru to the next.
case 'F':
this.degree=(this.degree-32)* (5/9);
this.degreeUnit= 'C';
break;
case 'K':
this.degree=this.degree-273.15;
this.degreeUnit= 'C';
break;
default:
this.degreeUnit= 'C';
A couple other problems.
One problem is (5/9). That is integer math and results in 0. Try (5./9)
And override toString in your temperature class to print the values when you specify the object. Something like:
@Override
public String toString() {
return degree + " " + degreeUnit;
}
</details>
# 答案2
**得分**: 0
"break;"命令在您的switch语句中缺失。请在您的switch语句中添加"break;"。以下是来自W3school(https://www.w3schools.com/java/java_switch.asp)的示例:
```java
switch(expression) {
case x:
// 代码块
break;
case y:
// 代码块
break;
default:
// 代码块
}
英文:
"break;" command is missing in your switch statement. Please add "break;" to your switch statement .Example from W3school( https://www.w3schools.com/java/java_switch.asp)
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论