Convert Fahrenheit temperature to Celsius and Kelvin.

weixin_52389260 2021-03-27 10:13:53
输出的结果与手算的结果不一致,不知道是哪里出现了错误。向各位大神们求助!

import java.text.DecimalFormat;
import java.util.Scanner;

class Temperature {
private static double fTemp;
Temperature(){
double fTemp;
}
//Create one field
public static void main(String[] args) {
double fTemp;
}
//A method named toCelsius that returns the temperature (value of fTemp) in Celsius
public static double toCelsius(){
DecimalFormat df = new DecimalFormat("0.00");
String a = df.format((double) 5/9);
double b = Double.parseDouble(a);
double result = (fTemp - 32) * b;
return result;
}

//A method named toKelvin that returns the temperature (value of fTemp) in Kelvin
public static Double toKelvin(){
DecimalFormat df = new DecimalFormat("0.00");
String a = df.format((double) 5/9);
double b = Double.parseDouble(a);
double result = (fTemp + 459.67) * b;
return result;
}
}

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

Temperature demo = new Temperature();
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter the Fahrenheit temperature: ");
double fTemp = Double.parseDouble(keyboard.nextLine());

//Convert input temperature to be rounded to 2 decimal places.
DecimalFormat df = new DecimalFormat("#.00");

System.out.print("Celsius Temperature: ");
//Print the temperature in Celsius using the toCelsius method of Temperature object
System.out.println(df. format(Temperature.toCelsius()));

System.out.print("Kelvin Temperature: ");
//Print the temperature in Kelvin using the toKelvin method of Temperature object
System.out.println(df. format(Temperature.toKelvin()));
}
}


下面是要求
引用
Temperature Class (Temperature.java)
• One field, a double, named fTemp. • One constructor that accepts one argument.
o The constructor should assign the value of this argument to fTemp. • A method named toCelsius that returns the temperature (value of fTemp) in Celsius.
F to C conversion: (fTemp - 32) * (5/9)
• A method named toKelvin that returns the temperature (value of fTemp) in Kelvin.
F to K conversion: (fTemp + 459.67) * (5/9)
• Neither the toCelsius or toKelvin methods should alter the value of the fTemp field.
TemperatureDemo Class (TemperatureDemo.java)
This is the class that will demo a Temperature object; thus, it contains the main method.
In the main method:
• Prompt the user to enter a temperature in Fahrenheit.
• Construct an instance of your Temperature object
o The user’s input value is passed as the argument to the constructor
• Using the Temperature object's toCelsius, and toKelvin methods, print:
o The temperature in Celsius.
o The temperature in Kelvin.
o All output should be rounded to 2 decimal places.


引用
Sample Input/Output
Please enter the Fahrenheit temperature: 98.6
Celsius Temperature: 37.00
Kelvin Temperature: 310.15
...全文
92 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

51,408

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧