C语言 简单程序找错 在线等,谢谢

来源:百度知道 编辑:UC知道 时间:2024/05/14 15:17:45
#include "stdio.h"
#include "math.h"
void main()
{int k,s1=0,s2=0;
k=1;
while(k<10)
{if(k%2==0) s2+=s2+k;
else s1=s1+k;k++;}
printf("%d\n%d\n",s1,s2);getch();}

输出,10以内的所有奇数和及偶数和,但是输出结果是25的52,,结果是错的呀

s2+=s2+k? 错了, 应该是
s2=s2+k或者
s2+=k

#include "stdio.h"
#include "math.h"
void main()
{int k,s1=0,s2=0;
k=1;
while(k<10)
{if(k%2==0) s2+=s2+k; //改为: {if(k%2==0) s2+=k;
else s1=s1+k;k++;}
printf("%d\n%d\n",s1,s2);getch();}

s2+=s2+k这句就不对了,您应改为:s2=s2+k或者改为:s2+=k,只有这两种表达是正确的,像您那样写法是不对的.