Java语言程序设计 例题6.8(摄氏度和华氏度的转换)

6.8 (Conversions between Celsius and Fahrenheit) Write a class that contains the following

two methods:

/** Convert from Celsius to Fahrenheit */

public static double celsiusToFahrenheit(double celsius)

/** Convert from Fahrenheit to Celsius */

public static double fahrenheitToCelsius(double fahrenheit)

The formula for the conversion is:

fahrenheit = (9.0 / 5) * celsius + 32

celsius = (5.0 / 9) * (fahrenheit – 32)

Write a test program that invokes these methods to display the following tables:

celsius fahrenheit fahrenheit celsius

40.0 104.0 120.0 48.89

39.0 102.2 110.0 43.33

32.0 89.6 40.0 4.44

31.0 87.8 30.0 -1.11

6.8(摄氏度和华氏度之间的转换)编写一个包含以下内容的类

两种方法:

/**从摄氏度转换为华氏度*/

public static double celsiusToFahrenheit(double celsius)

/**从华氏温度转换为摄氏温度*/

public static double fahrenheitToCelsius(double fahrenheit)

换算公式为:

华氏度=(9.0/5)*摄氏度+32

摄氏度=(5.0/9)*(华氏度–32)

编写一个测试程序,调用这个两个方法来显示如下表格:
摄氏度 华氏度 华氏度 摄氏度
40.0 104.0 120.0 48.89
39.0 102.2 110.0 43.33

32.0 89.6 40.0 4.44
31.0 87.8 30.0 -1.11

代码如下:


public class Unite6Test8 
{
	public static void main(String[] args) 
	{
		Change change = new Change();
		System.out.println("Celsius\t\tFahrenheit\t\tFahrenheit\t\tCelsius");
		for(double i = 40,j = 120;i >= 31 && j >= 30;i -= 1,j -= 10)
            System.out.printf("%.1f\t\t%.1f\t\t\t%.1f\t\t\t%.2f\n", i, change.celsiusToFahrenheit(i), j, change.fahrenheitToCelsius(j));
    }

}
class Change
{
	public static double celsiusToFahrenheit(double celsius)
	{
		double Fahrenheit = (9.0/5)*celsius+32;
		return Fahrenheit;
	}
	public static double fahrenheitToCelsius(double fahrenheit) 
	{
		double Celsius = (5.0/9)*(fahrenheit-32);
		return Celsius;
	}
}

结果如下:

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

差劲的厉害了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值