java写一个将摄氏温度转换成华氏温度的程序
时间: 2023-05-26 11:07:11 浏览: 92
以下是一个将摄氏温度转换成华氏温度的Java程序:
```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 = (9.0 / 5) * celsius + 32;
System.out.println(celsius + " degrees Celsius is equal to " + fahrenheit + " degrees Fahrenheit.");
}
}
```
该程序使用了一个Scanner对象来读取用户输入的摄氏温度,并将其转换为华氏温度。程序的主要逻辑在第6行,其中使用了公式 C = (F - 32) * (5/9) 将华氏温度转换成摄氏温度。最后,程序输出将摄氏温度转换成华氏温度的结果。
相关问题
java中编写一个程序将摄氏温度转换为华氏温度后输出
当然,很高兴为你编写这个程序。以下是 Java 中将摄氏温度转换为华氏温度的代码:
```java
import java.util.Scanner;
public class TemperatureConverter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入摄氏温度:");
double celsius = scanner.nextDouble();
double fahrenheit = celsius * 1.8 + 32.0;
System.out.println("华氏温度为:" + fahrenheit);
}
}
```
你可以将这段代码保存为 TemperatureConverter.java 并在命令行中运行它。它将提示你输入摄氏温度并输出相应的华氏温度。
将摄氏温度转换成华氏温度
以下是将摄氏温度转换为华氏温度的Java代码示例:
```java
import java.util.Scanner;
public class C_into_F {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a degree in Celsius:");
double Celsius = sc.nextDouble(); // Celsius摄氏温度
double Fahrenheit; //Fahrenheit华氏温度
Fahrenheit = (9.0/5) * Celsius + 32;
System.out.println(Celsius + " Celsius is " + Fahrenheit + " Fahrenheit");
}
}
```
请注意,上述代码需要用户输入摄氏温度,然后通过转换公式将其转换为华氏温度,并将结果输出。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)