我想利用用户输入‘Y' or 'N' 来控制程序是否继续进行
```c
#include
#include
#include
int main()
{
srand(time(NULL));
int magic = rand()%100 + 1;
int guess = 0;
int n = 0;
printf("Please enter a number.\n");
while(n<7)
{
scanf("%d", &guess);
if(guess < magic)
{
printf("Too low.\n");
}
else if(guess > magic)
{
printf("Too high.\n");
}
else
{
printf("Good,you are right.\n");
}
n++;
while(getchar() != '\n')
{
;
}
printf("Do you want to continue?\nY or N.\n");
char ch;
scanf("%c", &ch);
if(ch=='Y')
{
continue;
}
if(ch=='N')
{
break;
}
else
{
printf("Wrong.Please enter 'Yes' or 'No'. \n");
}
}
return 0;
}
###### 运行结果及报错内容
但是输入 N加回车可以反应, 输入Y+回车就无反应
###### 我的解答思路和尝试过的方法
没有头绪
###### 我想要达到的结果
你输入Y和N之后,也没有print任何东西,可不无反应吗
程序直接回到while的起点,又要你输入guess了
你把printf("Please enter a number.\n");这句放到while里面来
输入什么东西无反应啊???????界面截图看一下
你必须先输入一个整数后,才可以输入字母Y,N
int main()
{
srand(time(NULL));
int game = 1;
while (game)
{
int magic = rand() % 100 + 1;
int guess = 0;
int n = 0;
printf("Please enter a number.\n");
while (n < 7)
{
scanf("%d", &guess);
if (guess < magic)
{
printf("Too low.\n");
}
else if (guess > magic)
{
printf("Too high.\n");
}
else
{
printf("Good,you are right.\n");
break; //赢了直接跳出循环
}
n++;
}
if (n == 7)
printf("lose!\n");
//
printf("Do you want to continue?\nY or N.\n");
while (1)
{
char ch;
ch = getchar();
if (ch == 'Y' || ch == 'y')
{
break;
}
else if (ch == 'N' || ch == 'n') //
{
game = 0;
break;
}
else if (ch == '\n')
continue;
else
{
printf("Wrong.Please enter 'Yes' or 'No'. \n");
}
}
}
return 0;
}