#include<stdio.h>
int main(void)
{
int guess = 1;
char response;
printf("Pick an integer from 1 to 100. I will try to guess ");
printf("it.\nRespond with a y if my guess is right and with");
printf("\nan n if it is wrong.\n");
printf("Uh...is your number %d?\n", guess);
while ((response=getchar()) != 'y')
{
if (response == 'n')
printf("Well, then, is it %d?\n", ++guess);
else
printf("Sorry, I understand only y or n.\n");
while (getchar() != '\n')
continue;
}
printf("I knew I could do it!\n");
return 0;
}
请教,这两个代码的区别是什么?如果输入no sir,getchar()是什么读取字符的,按照上面的代码,是while ((response=getchar()) != 'y')判断response=n,还是response=no sir?
#include<stdio.h>
int main(void)
{
int guess = 1;
char response;
printf("Pick an integer from 1 to 100. I will try to guess ");
printf("it.\nRespond with a y if my guess is right and with");
printf("\nan n if it is wrong.\n");
printf("Uh...is your number %d?\n", guess);
while (getchar() != 'y')
{
printf("Well, then, is it %d?\n", ++guess);
while (getchar() != '\n')
continue;
}
printf("I knew I could do it!\n");
return 0;
}