华氏温度和摄氏温度的转换用Java写代码
时间: 2023-05-25 18:04:32 浏览: 229
java实验2.2(摄氏温度和华氏温度的转换)
4星 · 用户满意度95%
以下是将华氏温度转换为摄氏温度和将摄氏温度转换为华氏温度的Java代码示例:
将华氏温度转换为摄氏温度:
```java
import java.util.Scanner;
public class FahrenheitToCelsius {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a temperature in Fahrenheit: ");
double fahrenheit = input.nextDouble();
double celsius = (fahrenheit - 32) * 5 / 9;
System.out.println(fahrenheit + " degrees Fahrenheit is equal to " + celsius + " degrees Celsius");
}
}
```
将摄氏温度转换为华氏温度:
```java
import java.util.Scanner;
public class CelsiusToFahrenheit {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a temperature in Celsius: ");
double celsius = input.nextDouble();
double fahrenheit = celsius * 9 / 5 + 32;
System.out.println(celsius + " degrees Celsius is equal to " + fahrenheit + " degrees Fahrenheit");
}
}
```
阅读全文