你好,我是C语言的新手,正在尝试编写一个要求用户输入一大堆数字的程序。我想循环scanf
,这样它就会一直问,当用户输入"0"时,它会停止,读取输入的偶数和奇数,并分别计数。现在我让它在用户按下"Enter"后一直要求输入新的数字,但是当我输入"0"时,它只是一直要求输入更多的数字,并且没有停止。我做错了什么?就像我之前说的,我是一个新手,所以婴儿语是最好的。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void) {
int number_of_integers, sum = 0, i, integer;
char user_name[128];
printf("What is your name?n");
scanf("%s", user_name);
printf("nEnter any real numbers followed by ENTER.n");
while (integer != 0) {
scanf("%s", &integer);
if (integer == 0)
break;
}
printf("%s, the numbers you entered are broken down as follows:n", user_name);
return 0;
}
正如一位评论者所指出的,我们不是家庭作业或辅导服务,但我通过提供一个我刚刚编造的实际工作示例来帮助您。现在就看你怎么做了。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void) {
int max=100;
int integer;
int even[max];
int odd[max];
int index=0;
int evencount=0;
int oddcount=0;
char user_name[128];
printf("What is your name?n");
scanf("%s", user_name);
printf("nEnter any real numbers followed by ENTER.n");
while (index < max){
scanf("%d", &integer);
if (integer == 0)
break;
if ((integer % 2) == 0){
evencount++;
even[evencount]=integer;
}else{
oddcount++;
odd[oddcount]=integer;
}
index++;
}
printf("%s, the %d numbers you entered are broken down as follows:n", user_name,index);
printf("%d odd integer(s):n",oddcount);
while (oddcount > 0){
printf("%dn",odd[oddcount]);
oddcount--;
}
printf("%d even integer(s):n",evencount);
while (evencount > 0){
printf("%dn",even[evencount]);
evencount--;
}
return 0;
}