代码是正确的。输入0才能退出循环。不过这代码写得不是很好,求奇数之和,绕那么多弯子实在没有必要。给你提供一个作参考……
//#include "stdafx.h"//vc++6.0加上这一行.
#include "stdio.h"
int even(int n){
return n&1 ? n : 0;
}
int main(void){
int n,sum=0;
printf("Input integers: ");
while(scanf("%d",&n),n>0)
sum+=even(n);
printf("The sum of the odd numbers is %d\n",sum);
return 0;
}
//////这个学过吧?
//#include "stdafx.h"//vc++6.0加上这一行.#include "stdio.h"
int even(int n){
if(n%2!=0) return n;
else return 0;
}
int main(void){
int n,sum=0;
printf("Input integers: ");
scanf("%d",&n);
while(n>0){
sum+=even(n);
scanf("%d",&n);
}
printf("The sum of the odd numbers is %d\n",sum);
return 0;
}