conversion toCelsius() 方法未正常工作

huangapple go评论87阅读模式
英文:

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

&quot;break;&quot;命令在您的switch语句中缺失。请在您的switch语句中添加&quot;break;&quot;。以下是来自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
}

huangapple
  • 本文由 发表于 2020年9月20日 00:37:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63971079.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定