#include <stdio.h>
int main (void)
{
float heightcm;
float feet;
float inch;
printf("Enter height in centimeters to convert \n");
scanf("%f", &heightcm);
while (heightcm > 0)
{
feet = heightcm*0.033;
inch = heightcm*0.394;
printf("\n %0.1f cm = %0.2f feet and %0.2f inches \n", heightcm,feet,inch);
}
return 0;
}
最佳答案
你做了一个无限循环:
while (heightcm > 0) // if user enters a height > 0 we get in
{
feet = heightcm*0.033; // update feet
inch = heightcm*0.394; // update inches
// print the result
printf("\n %0.1f cm = %0.2f feet and %0.2f inches \n", heightcm,feet,inch);
}
循环中的任何地方 heightcm 都没有改变,这意味着它总是 > 0 并且您的函数将永远循环并且永远不会终止。 if 检查在这里更有意义:
if (heightcm > 0) // if user enters a height > 0 we get in
{
feet = heightcm*0.033; // update feet
...
或者您可以使用 while 循环并不断请求更多输入:
while (heightcm > 0)
{
printf("Enter height in centimeters to convert \n");
scanf("%f", &heightcm);
...