任务要求:

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.
Be sure to use comments to document your code.

Sample Input/Output
Please enter the Fahrenheit temperature: 98.6
Celsius Temperature: 37.00 Kelvin Temperature: 310.15

记录试错过程:

/***This program can convert a user's input for Fahrenheit temperature to Celsius and Kelvin.*/import java.text.DecimalFormat;import java.util.Scanner;class Temperature {private static double fTemp;//private static//double fTemp;//Temperature(double fTemp){//  tem = fTemp;public Temperature(double t) {fTemp = t;}//Create one field//public static void main(String[] args) {//double fTemp;//Instantiate a Temperature object//Temperature demo = new Temperature();//}//A method named toCelsius that returns the temperature (value of fTemp) in Celsiuspublic static double toCelsius(){//Scanner show1 = new Scanner(System.in);//System.out.println("Please enter the Fahrenheit temperature: ");//double fTemp = Double.parseDouble(show1.nextLine());double result = (fTemp - 32) * (5.0/9.0);return result;}//A method named toKelvin that returns the temperature (value of fTemp) in Kelvinpublic static double toKelvin(){//Scanner show2 = new Scanner(System.in);//System.out.println("Please enter the Fahrenheit temperature: ");//double fTemp = Double.parseDouble(show2.nextLine());double result = (fTemp + 459.67) * (5.0/9.0);return result;}
}class TemperatureDemo {public static void main(String[] args) {//Prompt the user to enter a temperature in Fahrenheit.Scanner keyboard = new Scanner(System.in);System.out.println("Please enter the Fahrenheit temperature: ");double fTemp = Double.parseDouble(keyboard.nextLine());//Instantiate a Temperature objectTemperature demo = new Temperature(fTemp);//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 objectSystem.out.println(df. format(Temperature.toCelsius()));System.out.print("Kelvin Temperature: ");//Print the temperature in Kelvin using the toKelvin method of Temperature objectSystem.out.println(df. format(Temperature.toKelvin()));}
}

P.S. 被“//”的绝大部分为“待用”部分——意为之前尝试过可行,但正在寻找更好方式的足迹。

可以看出,在class Temperature { private static double fTemp; }中所花费大量时间的尝试。目前还有一个对于此处的疑问:为什么语句是这样,而不是直接的double fTemp呢?(因为这一class的其它method引用不了。可是为什么呢?)(待今后的我来回答~)

重大发现:
当我把
Temperature demo = new Temperature(fTemp);
注释掉的时候,输出结果为:

Please enter the Fahrenheit temperature:
98.6
Celsius Temperature: -17.78
Kelvin Temperature: 255.37

数据不对,原因未知。(待未来的我回答)

终稿:

/***This program can convert a user's input for Fahrenheit temperature to Celsius and Kelvin.*/import java.text.DecimalFormat;import java.util.Scanner;class Temperature {private static double fTemp;public Temperature(double t) {fTemp = t;}//A method named toCelsius that returns the temperature (value of fTemp) in Celsiuspublic static double toCelsius(){double result = (fTemp - 32) * (5.0/9.0);return result;}//A method named toKelvin that returns the temperature (value of fTemp) in Kelvinpublic static double toKelvin(){double result = (fTemp + 459.67) * (5.0/9.0);return result;}
}class TemperatureDemo {public static void main(String[] args) {//Prompt the user to enter a temperature in Fahrenheit.Scanner keyboard = new Scanner(System.in);System.out.println("Please enter the Fahrenheit temperature: ");double fTemp = Double.parseDouble(keyboard.nextLine());//Instantiate a Temperature objectTemperature demo = new Temperature(fTemp);//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 objectSystem.out.println(df. format(Temperature.toCelsius()));System.out.print("Kelvin Temperature: ");//Print the temperature in Kelvin using the toKelvin method of Temperature objectSystem.out.println(df. format(Temperature.toKelvin()));}
}

撒花~~~

【记录贴】IPP3. Convert a Fahrenheit temperature to Celsius and Kelvin.相关推荐

  1. 瑞利散射 拉曼散射 米散射_使用Cartopy的时移散射图可视化

    瑞利散射 拉曼散射 米散射 简介(我们将创建的内容): (Introduction (what we'll create):) Cartopy is your choice of library if ...

  2. 《Go语言圣经》学习笔记 第二章 程序结构

    Go语言圣经学习笔记 第二章 程序结构 目录 命名 声明 变量 赋值 类型 包和文件 作用域 注:学习<Go语言圣经>笔记,PDF点击下载,建议看书. Go语言小白学习笔记,几乎是书上的内 ...

  3. STM32cube HAL库两条命令实现i2c通信---Nucleo L476RG用I2C实现tmp117模块温度读取并串口打印

    用stm32 cubemx默认配置i2c1和urart1,本例子是用硬件i2c非模拟i2c /* I2C1 GPIO Configuration PB6 ------> I2C1_SCLPB7 ...

  4. Go语言圣经阅读-第二周

    Go语言圣经阅读-第二周 2.4. 赋值 使用赋值语句可以更新一个变量的值,最简单的赋值语句是将要被赋值的变量放在=的左边,新值的表达式放在=的右边. x = 1 // 命名变量的赋值 *p = tr ...

  5. python简单温度转换,python实现简单温度转换的方法

    本文实例讲述了python实现简单温度转换的方法.分享给大家供大家参考.具体分析如下: 这是一段简单的python代码,用户转换不同单位的温度,适合初学者参考 def c2f(t): return ( ...

  6. 温度转换PHP,华氏温度转换为摄氏度的程序

    在华氏温度为n的情况下,面临的挑战是将给定温度转换为摄氏度并显示出来. 示例Input 1-: 132.00 Output -: after converting fahrenheit 132.00  ...

  7. 【go语言圣经】习题答案 第二章

    第二章练习题答案 2.1 添加Kelvin绝对温度的转换 2.2 长度转换 2.3 2.4 重写PopCount函数,用一个循环代替单一的表达式 2.5 用移位算法重写PopCount函数 2.1 添 ...

  8. 2021-12-3 ds18x20包装库 9元钱的 ds18B20 esp8266 micropython 记录

    这也是一个9元全套的货色,带一个18B20温度传感器,带一个ESP01芯片,电压输入是3.3-12V电压,可以直接上电池供电都不用转换的. 这位模块大兄弟不但价格便宜而且体长苗条,2.5厘米的身形比拇 ...

  9. 钟表维修管理系统技术解析 维修记录(五)

    维修记录模块 维修记录就是记录钟表的维修内容和所使用的配件,使用配件时会生成出库记录,记录库存的数量变化.如果单据维修完成,也可以执行完成维修的操作. 实现思路:       当点击维修记录时跳转到维 ...

最新文章

  1. 2022-2028年中国六氟化硫行业市场研究及前瞻分析报告
  2. c++成员函数的重载、覆盖、隐藏区别
  3. Flutter控件--Switch 和 SwitchListTile
  4. MySQL:给表的某个字段添加唯一性约束
  5. connectionstring mysql_Entity Framework 6 自定义连接字符串ConnectionString连接MySQL
  6. php 添加音乐,PHP网站插入音乐
  7. 三菱mode bus tcp通讯_绍兴三菱MR-J4-70B
  8. 实习分享 | 来康康字节小姐姐的经验
  9. Portage百度百科
  10. 密码字典生成工具—Crunch的使用
  11. plsqldev使用指南
  12. 商品期货日内 Dual Thrust 交易策略
  13. Rider+EmmyLua lua代码高亮设置
  14. 云计算学习笔记3——分布式通信
  15. CUDA Occupancy Calculator中计算占用率
  16. Python随机函数
  17. Smartbi的下载安装教程
  18. 计算机ps特效教程,计算机一级photoshop给照片制作半素描效果教程
  19. Web前端:2022年最佳Javascript动画库
  20. Coherent Reconstruction of Multiple Humans from a Single Image运行代码

热门文章

  1. AV1基于机器学习的变换块快速划分
  2. 计算机启动时报警原因,电脑出现报警声怎么办?电脑出现报警声的原因
  3. 统计学(一): Z 分数 正态分布 (附 Python 实现代码) --Z 检验先修; Z 分数与正态分布两者关系; Z 分数与百分位数的异同;面试要点(以心理学实验为舟)
  4. 均匀分布 卡方分布_深度学习需要掌握的13个概率分布(附代码)
  5. HTTP 状态消息 200 302 304 403 404 500 分别表示什么?
  6. ZOJ Gold Coins2345
  7. node js 写按键精灵_带有按键的Node.js Raw模式
  8. Steven-Java-运算符号(简单)
  9. 玉米社:网站seo站外优化技巧、注意事项
  10. 名悦集团:冬季汽车保养怎么做才到位