Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 转换为Elsius()方法工作不正常_Java_Methods - Fatal编程技术网

Java 转换为Elsius()方法工作不正常

Java 转换为Elsius()方法工作不正常,java,methods,Java,Methods,我编写了一个将度单位转换为摄氏度的方法,但是当我在main方法中运行它时,它给出了错误的输入。 理想情况下,212F的温度为100C;然而,我得到的输出是-273.15。哦,天哪。有什么想法吗? *编辑-我错过了休息;第一次,所以我明白了。插入后,我得到了一个0.0的数字,下面是我最新的代码 public class Temperature { private double degree; private char degreeUnit; public Tem

我编写了一个将度单位转换为摄氏度的方法,但是当我在main方法中运行它时,它给出了错误的输入。 理想情况下,212F的温度为100C;然而,我得到的输出是-273.15。哦,天哪。有什么想法吗? *编辑-我错过了休息;第一次,所以我明白了。插入后,我得到了一个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/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());
    }
}

您需要在案例之后输入
break
语句。否则,它们就会掉到下一个

        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';
        
还有几个问题

一个问题是(5/9)。这是整数数学,结果为0。试试看(5/9)

并重写温度类中的toString,以便在指定对象时打印值。比如:

   @Override
    public String toString() {
        return degree + " " + degreeUnit;
    }



您需要在案例之后输入
break
语句。否则,它们就会掉到下一个

        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';
        
还有几个问题

一个问题是(5/9)。这是整数数学,结果为0。试试看(5/9)

并重写温度类中的toString,以便在指定对象时打印值。比如:

   @Override
    public String toString() {
        return degree + " " + degreeUnit;
    }


switch语句中缺少“break;”命令。请在switch语句中添加“break;”。W3school()中的示例

开关(表达式){ 案例十: //代码块 打破 案例y: //代码块 打破 违约: //代码块 }switch语句中缺少“break;”命令。请在switch语句中添加“break;”。W3school()中的示例

开关(表达式){ 案例十: //代码块 打破 案例y: //代码块 打破 违约: //代码块
}

我很感激;我错过了复旦节。在我添加它们之后,我得到了0。也许我的代码中还有其他错误?问题是(5/9)。这是整数数学,结果为0。试试(5/9)。我没想到。我会努力的!你启发了我。非常感谢。我很感激,;我错过了复旦节。在我添加它们之后,我得到了0。也许我的代码中还有其他错误?问题是(5/9)。这是整数数学,结果为0。试试(5/9)。我没想到。我会努力的!你启发了我。非常感谢。