第3个回答 2019-05-05
你可以检查scanf的返回值,如果不等于1说明输入是afsaf之类的字符串,这个时候scanf不会读入这些串,你后面要用fgets来读取来抛弃输入流的数据。
int
i;
int
result;
char
buf[80];
while
(1)
{
result
=
scanf("%d",
&i);
if
(result
==
1
&&
i
>=
1
&&
i
<=4)
break;
else
{
/*
抛弃用户的输入
*/
fgets(buf,
80,
stdin);
printf("Error,
Please
Input
again!");
}
}