0
我一直在编写一个程序,它接受输入并检查数字是偶数还是奇数,并在输入是字符而不是数字时输出错误消息我的初始代码为:在while循环中用scanf()检查输入类型
int main()
{
int x;
int check = scanf("%d", &x);
printf("input: ");
while(check != 1){ //means that the input is inappropriate
printf("Error!: unexpected input\n");
printf("input: ");
check = scanf("%d", &x);
}
if(x%2 == 0){
printf("It's even\n");
}else{
printf("It's odd\n");
}
return 0;
}
当我运行一个无限循环的印刷
“错误!:意外输入\ n”个 但是当我把下面的语句在while循环中它工作正常的说法是:scanf("%s",&x);
有人可以解释这个行为?