华氏温度转换摄氏温度的代码
时间: 2023-05-17 11:06:19 浏览: 54
以下是将华氏温度转换为摄氏温度的 Python 代码:
```python
fahrenheit = float(input("请输入华氏温度:"))
celsius = (fahrenheit - 32) * 5 / 9
print("摄氏温度为:", celsius)
```
如果您需要 Lua 代码,可以尝试以下代码:
```lua
function fahrenheit_to_celsius(fahrenheit)
local celsius = (fahrenheit - 32) * 5 / 9
return celsius
end
-- 示例
local fahrenheit = 68
local celsius = fahrenheit_to_celsius(fahrenheit)
print("摄氏温度为:", celsius)
```
希望能对您有所帮助!
相关问题
输入华氏温度转换摄氏温度c代码
以下是将华氏温度转换为摄氏温度的 C 代码:
```c
#include <stdio.h>
int main() {
float fahrenheit, celsius;
printf("请输入华氏温度:");
scanf("%f", &fahrenheit);
celsius = (fahrenheit - 32) * 5 / 9;
printf("摄氏温度为:%.2f\n", celsius);
return 0;
}
```
请注意,这只是一个简单的示例代码,可能需要根据实际情况进行修改和优化。
华氏温度转换摄氏温度整数
根据提供的引用内容,可以使用以下公式将华氏温度转换为摄氏温度:
摄氏温度 = (华氏温度 - 32) / 1.8
如果要将结果保留为整数,可以使用Java中的Math.round()函数进行四舍五入。具体实现可以参考引用中的代码示例。如果使用Python编写程序,可以使用以下代码实现:
```
f = float(input("请输入华氏温度:"))
c = round(5 / 9 * (f - 32))
print("华氏温度:%.1f,对应的摄氏温度:%d" % (f, c))
```
其中,round()函数可以将结果四舍五入为整数,%.1f表示保留一位小数输出,%d表示输出整数。