我在使用用户输入问题的这个简单 for 循环中遇到了一些麻烦。问题要我创建一个表,将华氏度转换为摄氏度,使用 scanf 为表的起始值、结束值和增量值获取用户输入值。我只编码了 2 周,我刚刚开始循环,但这似乎应该有效。谢谢!这是我的代码:
#include <stdio.h>
int main (void)
{
int f, c, f_min, f_max, i;
printf("Enter the minimum (starting) temperature value: ");
scanf("%d", &f_min);
printf("Enter the maximum (ending) temperature value: ");
scanf("%d", &f_max);
printf("Enter the table increment value: ");
scanf("%d", &i);
for (f = scanf("%d", &f_min); f <= scanf("%d", &f_max); f = f + scanf("%d", &i))
{
c = ((f - 32.0) * (5.0 / 9.0));
// printf("Degrees in C is: %d");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
您scanf在循环初始值设定项、循环保护和循环增量计数内部调用,这意味着程序在循环开始时、每次循环迭代开始时以及每次循环迭代结束时都在等待您的输入. 您也不是比较值f_max,i而是比较 的返回值scanf,这是它从输入字符串成功填充的格式说明符的数量,而不是读取的值。
你已经有了你想要的值,f_min,f_max和i,只需在循环中使用这些值:
for(f = f_min; f <= f_max; f+=i)
Run Code Online (Sandbox Code Playgroud)