#include <stdio.h>
int main()
{
char target ='D';
while(true)
{
char min = 'A';
char max = 'Z';//初始范围
int count = 0;//猜测次数
int guess = 0;
printf("please input a character between %c and %c\n",min,max);
fflush(stdin);//清空输入缓存,以便不影响后面输入的数。比如你逐个输入字符,他帮你缓冲掉你每输入一个字符后面所敲的回车键。否则回车也会被当成字符保存进去。
scanf("%c",&guess); // 获取猜测的数字
++count;
if(target == guess) //猜中
{
printf("YOU WIN!\nyou have guessed %d times in total.\n",count);
break;
}
else if(target > guess) //目标比猜的数字大
{
min = guess;
printf("the target is larger than %c\n",guess);
}
else //目标比猜的数字小
{
max = guess;
printf("the target is less than %c\n",guess);
}
}
return 0;
}
代码如上,万望采纳。