我编写了一个程序,如下所示:
#include <stdio.h>
int main() {
int i, choice, a, mul=1, j;
printf("Enter a number: ");
scanf("%d ", &i);
printf("\nEnter corresponding number to perform given operation:");
printf("1) Factorial of the number");
printf("2) Check if prime or not");
printf("3) Check whether Odd or Even");
printf("4) Exit");
scanf("%d ", &choice);
switch (choice) {
case 1:
for( a=i; a>0; a--) {
mul*=a;
}
printf("\n%d! = %d", i, mul);
break;
case 2:
for(a=1; a<=((i+1)/2); a++) {
if((i%a)==0) {
j++;
}
}
if(j==1) {
printf("%d is a prime number",i);
} else {
printf("%d is NOT a prime number",i);
}
break;
case 3:
if((i%2)==0) {
printf("%d is EVEN", i);
} else {
printf("%d is ODD", i);
}
break;
case 4:
;
}
return 0;
}
运行程序时,它正确运行了前几行,即:
printf("Enter a number: ");
scanf("%d ", &i);
但是随后它停了下来。我按了几次Enter键,但似乎没有用。然后我只是输入“ 1”,然后它开始执行下一条语句。然后它要求choice
':
scanf("%d ", &choice);
我再次输入“ 1”,然后执行该情况。我不知道为什么程序在第一个scanf
语句之后停止。并在输入值后恢复。
输出屏幕如下:
Enter a number: 12
1
Enter the corresponding number to perform given operation:
1)Factorial of the number
2)Check if prime or not
3)Check whether Odd or Even
4)Exit1
12! = 479001600