华氏度和摄氏度的换算java
时间: 2024-06-13 09:03:37 浏览: 10
华氏度和摄氏度是温度的两种不同的度量单位,它们之间可以通过以下公式进行转换:
Celsius = (Fahrenheit - 32) * 5 / 9
Fahrenheit = (Celsius * 9 / 5) + 32
在Java中,可以使用Scanner类来获取用户输入的摄氏度,然后使用上述公式将其转换为华氏度。以下是一个示例代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double Celsius, Fahrenheit;
Celsius = scanner.nextDouble();
Fahrenheit = (9.0 / 5) * Celsius + 32;
System.out.printf("摄氏温度%.2f--华氏温度%.2f", Celsius, Fahrenheit);
}
}
另外,如果需要输出华氏度和摄氏度之间的转换表,可以使用循环语句来实现。以下是一个示例代码:
public class Main {
public static void main(String[] args) {
double fahr = 0, celsius;
System.out.println("华氏温度 摄氏温度");
while (fahr <= 80) {
celsius = (fahr - 32) * 5 / 9;
System.out.printf("%3.0f %6.1f\r\n", fahr, celsius);
fahr = fahr + 20;
}
}
}