【使用goto统计输入数的奇偶数量】2022-10-28
缘由C语言求奇偶个数,能用goto吗-编程语言-CSDN问答
从键盘输入一系列正整数,输入-1表示输入结束(-1本身不是输入的数据)。编写程序判断输入数据中奇数和偶数的个数。如果用户输入的第一个数据就是-1,则程序输出"over!"。否则。用户每输入一个数据,输出该数据是奇数还是偶数,直到用户输入-1为止,分别统计用户输入数据中奇数和偶数的个数。
#include <stdio.h>
#include <stdlib.h>
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没问题吧。
void 使用goto统计输入数的奇偶数量()
{//缘由https://ask.csdn.net/questions/7820341int n = 0, q = 0, o = 0;
ks:scanf_s("%d", &n);if (n == -1){if (q == 0 && o == 0)printf("over!\n");goto js;}else{if (n % 2 > 0)++q;else++o;goto ks;}
js:if (q || o){printf("The total number of odd is %d\n", q);printf("The total number of even is %d\n", o);}
}