代码可以这样写吗
#include<stdio.h>int main()
{ int i,sum,n;
n = 100;
sum = 0;
for (i = 1; i<=n; i++)
sum = sum+i;
printf("sum of number from 1 to %d is %d\n",n,sum);
int a,b,c;
a = 1;
b = 0;
c = 100;
while(a<=c)
{ b = b+1;
a = a++;
}
printf("n of number from 1 to %d is %d\n",c,b);
return 0;
}
想象中的运行结果
sum of number from 1 to 100 is 5050
n of number from 1 to 100 is 5050
实际上
sum of number from 1 to 100 is 5050
有什么办法可以不让它从中间就跳出运行吗 你这个 while 循环成死循环了
a = a++; 这一句 应该改为 a = a + 1; 或 a++; 2楼正解,不过即使修改后也看不到
n of number from 1 to 100 is 5050
而是
n of number from 1 to 100 is 100
另外打开编译警告看看吧,能减少不少问题
test.c:15:22: warning: multiple unsequenced modifications to 'a' [-Wunsequenced]
a = a++;
~^
1 warning generated. 哦吼,谢谢啦,我也发现了我while 语句那里b = b+1 应该改为b = b+a 才能看到我想要的结果
页:
[1]