温度输出编程怎么编的

时间:2025-01-23 13:06:05 游戏攻略

温度输出编程可以通过多种编程语言实现,以下是几种常见编程语言的示例代码:

C语言

```c

include

int main() {

float fahrenheit, celsius;

printf("请输入华氏温度: ");

scanf("%f", &fahrenheit);

celsius = (fahrenheit - 32) * 5 / 9;

printf("对应的摄氏温度为: %.2f\n", celsius);

return 0;

}

```

Python

```python

def celsius_to_fahrenheit(celsius):

fahrenheit = (celsius * 9 / 5) + 32

return fahrenheit

def fahrenheit_to_celsius(fahrenheit):

celsius = (fahrenheit - 32) * 5 / 9

return celsius

celsius = float(input("请输入摄氏温度: "))

fahrenheit = celsius_to_fahrenheit(celsius)

print("华氏温度为:", fahrenheit)

fahrenheit = float(input("请输入华氏温度: "))

celsius = fahrenheit_to_celsius(fahrenheit)

print("摄氏温度为:", celsius)

```

Java

```java

import java.util.Scanner;

public class TemperatureConverter {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("请输入摄氏温度: ");

double celsius = scanner.nextDouble();

double fahrenheit = celsiusToFahrenheit(celsius);

System.out.printf("华氏温度为: %.2f\n", fahrenheit);

System.out.print("请输入华氏温度: ");

fahrenheit = scanner.nextDouble();

celsius = fahrenheitToCelsius(fahrenheit);

System.out.printf("摄氏温度为: %.2f\n", celsius);

}

public static double celsiusToFahrenheit(double celsius) {

return (celsius * 9 / 5) + 32;

}

public static double fahrenheitToCelsius(double fahrenheit) {

return (fahrenheit - 32) * 5 / 9;

}

}

```

C++

```cpp

include

double celsiusToFahrenheit(double celsius) {

return (celsius * 9 / 5) + 32;

}

double fahrenheitToCelsius(double fahrenheit) {

return (fahrenheit - 32) * 5 / 9;

}

int main() {

double celsius, fahrenheit;

std::cout << "请输入摄氏温度: ";

std::cin >> celsius;

fahrenheit = celsiusToFahrenheit(celsius);

std::cout << "华氏温度为: " << fahrenheit << std::endl;

std::cout << "请输入华氏温度: ";

std::cin >> fahrenheit;

celsius = fahrenheitToCelsius(fahrenheit);

std::cout << "摄氏温度为: " << celsius << std::endl;

return 0;

}

```

这些示例代码展示了如何在不同的编程语言中实现温度转换功能。你可以根据自己的需求选择合适的编程语言进行开发。