这就是我写的。我猜它可能是我的while
循环的逻辑,但我无法发现它!任何帮助表示赞赏!谢谢。
#include <stdio.h>
#include <math.h>
//Open main function.
int main(void)
{
double new_area, area_total = 14000, area_uncut = 2500, rate = 0.02, years;
int count = 0;
printf("This program is written for a plot of land totaling 14000 acres, "
"with 2500 acres of uncut forest\nand a reforestation rate "
"of 0.02. Given a time period (years) this program will output a table\n"
"displaying the number acres reforested at the end of "
"each year.\n\n\n");
printf("Please enter a value of 'years' to be used for the table.\n"
"Values presented will represent the number acres reforested at the end of "
"each year:>> ");
scanf("%lf", &years);
years = ceil(years);
printf("\n\nNumber of Years\t\tReforested Area");
while (count <= years);
{
count = count + 1;
new_area = area_uncut + (rate * area_uncut);
printf("\n%1.0lf\t\t\t%.1lf", count, area_uncut);
area_uncut += new_area;
}
return 0;
}
答案 0 :(得分:4)
此行末尾有一个额外的;
:while (count <= years);
它被解析为while
循环的空体,导致它永远迭代,因为count
根本没有更新。
这是一种避免这种愚蠢错误的方法:使用Kernighan和Ritchie样式,{
位于行的末尾,开始控制块:
while (count <= years) {
count = count + 1;
new_area = area_uncut + (rate * area_uncut);
printf("\n%d\t\t\t%.1f", count, area_uncut);
area_uncut += new_area;
}
使用这种风格,额外的;
输入的可能性要小得多,并且更容易被发现为不协调。
另请注意,count
定义为int
,因此printf
格式也不正确。绝对编译时启用了更多警告。