我希望程序不断要求用户输入另一个字母,直到输入“y”或“n”。 while 循环无法正常运行。 这是我到目前为止的代码:
#include<stdlib.h>
int main(void)
{
char answer;
printf("Please enter a letter: ");
scanf("%c", &answer);
while (answer!= 'y' || answer!= 'n')
{
printf("Please enter another letter:");
scanf("%c", &answer);
}
printf("You entered either yes or no\n");
system("pause");
return 0;
}
最佳答案
这是一个固定版本。请注意包含 stdio.h
以及将 ||
修复为 &&
以及 "%c 之前的空格“
为 “%c”
。
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char answer;
printf("Please enter a letter: ");
scanf("%c", &answer);
while (answer != 'y' && answer != 'n')
{
printf("Please enter another letter:");
scanf(" %c", &answer);
}
printf("You entered either yes or no\n");
system("pause");
return 0;
}
关于c - 如何继续循环程序直到输入 'y' 或 'n',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46634713/