c - 如何继续循环程序直到输入 'y' 或 'n'

标签 c

我希望程序不断要求用户输入另一个字母,直到输入“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/

相关文章:

c++ - 客户端未通过套接字接收回数据 [传输端点已连接]

c - 我收到错误 : expected '=' , ',' 、 ';' 、 'asm' 或 '' __attribute_ _' before ' void'

c - 链表内存图

c - 尝试用 C 创建基本的二叉搜索树,但对 'unused variable' 感到困惑?

c - 我正在尝试运行这个简单的程序,但在 "expected ‘=’ token 之前出现此错误消息 ‘,’ 、 ‘;’ 、 ‘asm’ 、 ‘__attribute__’ 或 ‘<’ ”

来自麦克风的连续语音识别 pocketsphinx

c - 循环没有结束

c++ - 跟踪 pthread

c - 为什么我不能分配比 RAM 中可用的内存更多的内存?

c - 如何将所有元音从字符串移动到不同的数组