【发布时间】:2013-10-30 21:20:19
【问题描述】:
我正在尝试使用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 块。如果another == 1,程序再次运行。
我的问题是为什么我不能检查 while 条件中的字符。
【问题讨论】:
标签: c while-loop char scanf