从键盘输入一系列正整数,输入-1表示输入结束(-1本身不是输入的数据)。编写程序判断输入数据中奇数和偶数的个数。如果用户输入的第一个数据就是-1,则程序输出"over!"。否则。用户每输入一个数据,输出该数据是奇数还是偶数,直到用户输入-1为止,分别统计用户输入数据中奇数和偶数的个数。
#include
#include
main()
{
int n,ret,count_1=0,count_2=0;
printf("Please enter the number:\n");
ret=scanf("%d",&n);
while(ret==1&&n!=-1)
{
if(n%2==0)
{
printf("%d:even\n",n);
count_1++;
}
else
{
printf("%d:odd\n",n);
count_2++;
}
ret=scanf("%d",&n);
if(n==-1) goto end;
}
printf("over!\n");
end:
printf("The total number of odd is %d\n",count_2);
printf("The total number of even is %d\n",count_1);
return 0;
}
能运行,这样用goto没问题吧
goto也是循环要goto就全部goto。
可以的,只要这样写你的代码可以满足题目的要求即可
进来避免使用goto
#include <stdio.h>
int main()
{
int n, ret, count_1 = 0, count_2 = 0;
printf("Please enter the number:\n");
while (scanf("%d", &n) == 1 && n != -1)
{
if (n % 2 == 0)
{
printf("%d:even\n", n);
count_1++;
}
else
{
printf("%d:odd\n", n);
count_2++;
}
}
if (count_1 == 0 && count_2 == 0)
printf("over!\n");
else
{
printf("The total number of odd is %d\n", count_2);
printf("The total number of even is %d\n", count_1);
}
return 0;
}
为什么我的C没学过goto呢?
你这是不是太老了呀?