<分区>
我正在尝试使用 do-while
执行一个小程序。
#include<stdio.h>
#include<conio.h>
void main()
{
char another;
int num;
do
{
printf("Enter a number");
scanf("%d",&num);
printf("Square of %d id %d",num,num*num);
printf("Want to another another number y/n");
scanf("%c",&another);
}while(another=='y');
}
现在,当我尝试执行该程序时,它运行良好。我输入一个数字,它显示它的正方形。然后我看到 Want to enter another number y/n
。但是只要我按下任意键(y 或 n),程序就会在我按下 enter 键提供输入之前自行退出。我试了很多次都没有成功。
但如果我要求用户输入 1 或 2(代替 y/n),程序运行正常。在这种情况下,它需要一个整数输入并可以检查 while block 。如果 another == 1
,程序再次运行。
我的问题是为什么我不能检查 while
条件中的字符。