简体   繁体   English

使用Java中的对象进行温度转换

[英]Temperature conversion using objects in java

So i am currently writing a program using objects where the user enters an initial temperature, then the program has to compute it into Celsius, which would just be the user input, then to Fahrenheit and then to kelvin. 因此,我目前正在编写一个使用用户输入初始温度的对象的程序,然后该程序必须将其计算到摄氏温度(这只是用户的输入),然后是华氏度,然后是开尔文。 The class also has a single constructor that accepts an initial temperature provided as a double argument. 该类还具有单个构造函数,该构造函数接受作为双参数提供的初始温度。 If this argument is < -273.15 , set it to -273.15 . 如果此参数为< -273.15 ,则将其设置为-273.15 I thought I was on the right track but when i compiled it, it wasn't doing what i wanted, any tips on how I can fix it? 我以为自己是在正确的轨道上,但是当我编译它时,它没有按照我想要的去做,关于如何解决它的任何提示?

With this code, the output gives me 有了这段代码,输出给了我

Please enter the initial temperature: 20 请输入初始温度:20
The current temperature in Celsius is: 0.0 当前的摄氏温度是:0.0
The current temperature in Fahrenheit is: 32.0 华氏当前温度是:32.0
The current temperature in Kelvin is: 273.15 当前温度在开尔文是:273.15

which isn't right... any tips? 这不对...有什么提示吗?

//blueprint
public class TemperatureC{
    private double temperatureC;

    public TemperatureC(){
        if(temperatureC<-273.15){
            temperatureC = -273.15;}
        else{}
    }

    public void setC(double c){
        temperatureC = c;
    }
    public double getC(){return temperatureC;}
    public double getF(){return (temperatureC * 1.8) + 32;}
    public double getK(){return temperatureC + 273.15;}
}   



//code
import java.util.Scanner;

public class TemperatureTester{
    public static void main(String[] args){

        TemperatureC temp = new TemperatureC();

        double initialTemperature;
        double celsius=temp.getC();
        double fahrenheit=temp.getF();
        double kelvin=temp.getK();

        Scanner keyboard = new Scanner(System.in);

        System.out.print("Please enter the initial temperature: ");
        initialTemperature = keyboard.nextDouble();


        //TemperatureC temp = new TemperatureC();

        System.out.println("The current temperature in Celsius is: " + celsius);
        System.out.println("The current temperature in Fahrenheit is: "+fahrenheit);
        System.out.println("The current temperature in Kelvin is: "+kelvin);    
    }   
}

You are assigning the values of celsius , fahrenheit , and kelvin before you know the temperature value. 您需要先分配celsiusfahrenheitkelvin的值,然后再知道temperature值。 You want your tester to look more like this 您希望测试人员看起来更像这样

public static void main(String[] args) {
  TemperatureC temp = new TemperatureC();
  double initialTemperature;
  Scanner keyboard = new Scanner(System.in);
  initialTemperature = keyboard.nextDouble();
  temp.setC(initialTemperature);

  System.out.println("The current temperature in Celsius is: " + temp.getC());
  System.out.println("The current temperature in Fahrenheit is: "+temp.getF());
  System.out.println("The current temperature in Kelvin is: "+temp.getK());
}

So the operations are now done after the temperature of initialTemperature is set. 因此,现在在设置initialTemperature的温度之后initialTemperature完成操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM