【发布时间】:2017-09-19 19:02:27
【问题描述】:
我需要运行一个 while 循环来打印它的每一个结果,但不能让它正常工作。这是我取得的成就:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main ()
{
float fahrenheit;
int celsius;
printf("Table of calsius degrees in fahrenheit 1-100:");
celsius=0;
fahrenheit=32;
while (0 < celsius < 100)
{
celsius = (5.0 / 9.0) * (fahrenheit-32);
fahrenheit = ((9.0 / 5.0) * celsius )+ 32;
printf ("%f , %f\n", celsius,fahrenheit);
celsius ++;
}
if (celsius>=101){
printf("Jobs done");
}
return 0;
}
非常感谢任何帮助。目标是打印从 1 到 100 的每一个华氏温度和摄氏度。
【问题讨论】:
-
while (0 < celsius < 100)这不是你想的那样。你的意思是while(celsius > 0 && celsius < 100) -
正常工作是什么意思?它输出错误?是不是输出正确的结果?
-
开启完整警告,你应该会看到类似
comparison of constant 100 with boolean expression is always true -
另请注意:1)由于您将变量
Celsius初始化为0,因此您甚至都不会进入循环。 2)由于您每次都重新计算celsius,因此您可能会停止循环,因为您将失去精度并且最终不会四舍五入,最终会导致无限循环。 -
即使那是正确的语法,你也永远不会进入循环。你将
celsius设置为0,所以0 < celsius < 100第一次会是假的。
标签: c while-loop