将 5 个温度从华氏度转换为摄氏度 [英] converting 5 temperatures from fahrenheit to celsius
本文介绍了将 5 个温度从华氏度转换为摄氏度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
每天,气象站都会收到 5 个以华氏度表示的温度.编写一个接受华氏温度的程序,将其转换为摄氏温度并在屏幕上显示转换后的温度.5 次温度后,屏幕上应显示消息所有温度已处理"
Every day, a weather station receives 5 temperatures expressed in degree Fahrenheit. Write a program to accept temperature in Fahrenheit, convert it to Celsius and display the converted temperature on the screen. After 5 temperatures, the message ‘All temperatures processed’ should be displayed on the screen
我的回答如下
#include <stdio.h>
main()
{
int fah, cel;
printf("\nEnter 5 temperatures in Fahrenheit: ");
scanf("%d %d %d %d %d", &fah);
do
{
cel=(fah-32)*(5/9);
printf("\n These temperatures converted to Celsius are: %d \n", cel);
}
while(fah);
}
推荐答案
让我们一一解决 -
scanf("%d %d %d %d %d", &fah);
:你想扫描五个值,而你只使用一个%d代码>.其他四个值呢?您应该使用包含 5 个值的数组.
cel=(fah-32)*(5/9);
:您将 5 除以 9,这两个都是整数.一个整数除以另一个的结果,a/b,其中 a cel 将始终为 0.您必须将整数之一转换为浮点数/双精度数.- 如果您确实使用数组,则需要遍历数组的所有元素来处理它们.
scanf("%d %d %d %d %d", &fah);
: You want to scan five values while you are using only one%d
. What about the other four values ? You should use an array of 5 values.cel=(fah-32)*(5/9);
: You are dividing 5 by 9, both of which are integers. The result of dividing an integer by another, a/b, where a < b is 0. Hencecel
will always be 0. You have to cast one of the integers to a float/double.- If you do use an array, you need to loop over all elements of the array to process them.
我故意不提供所有这些的直接解决方案.我建议您查看并理解这些内容,因为它们是 C 编程的基础.
I am purposely not providing a direct solution to all these. I suggest you look up on these things and understand them, since they are very basic for programming in C.
这篇关于将 5 个温度从华氏度转换为摄氏度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文