首页 » 技术分享 » 第六章第八题(摄氏度和华氏度之间的转换)(Conversions between celsius and fahrenheit)

第六章第八题(摄氏度和华氏度之间的转换)(Conversions between celsius and fahrenheit)

 

第六章第八题(摄氏度和华氏度之间的转换)(Conversions between celsius and fahrenheit)

  • 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
    6.8(Conversions between celsius and fahrenheit)Write a class that contains the following two methods:
    public static double celsiusToFahrenheit(double 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
  • 参考代码:
package chapter06;

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

class Conversion{
    public static double celsiusToFahrenheit(double celsius) {
        return (9.0 / 5) * celsius + 32;
    }
    public static double fahrenheitToCelsius(double fahrenheit) {
        return (5.0 / 9) * (fahrenheit - 32);
    }
}
  • 结果显示:
Celsius		Fahrenheit		Fahrenheit	Celsius
-----------------------------------------------------------------
40.0		104.0			120.0		48.89
39.0		102.2			110.0		43.33
38.0		100.4			100.0		37.78
37.0		98.6			90.0		32.22
36.0		96.8			80.0		26.67
35.0		95.0			70.0		21.11
34.0		93.2			60.0		15.56
33.0		91.4			50.0		10.00
32.0		89.6			40.0		4.44
31.0		87.8			30.0		-1.11

Process finished with exit code 0

转载自原文链接, 如需删除请联系管理员。

原文链接:第六章第八题(摄氏度和华氏度之间的转换)(Conversions between celsius and fahrenheit),转载请注明来源!

0