读取用户输入时循环不会停止 [英] Looping won't stop when reading user input

查看:62
本文介绍了读取用户输入时循环不会停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使我输入n或N,此代码也无法停止循环.

This code cannot stop looping even when I enter n or N.

是什么原因造成的?

    char next = "";

    printf("Do u want continue\n");
    scanf("%c", &next);
    getchar();
    do
    {
        if (next=='y' ||next=='Y')
        {
            selection();
        }
        else
        {
            printf("invalid Please Enter again");
            scanf("%c", &next);
        }
    } while (next !='n'|| next!='N')

推荐答案

条件(next !='n'|| next!='N')始终为true.

如果next'n',则next!='N'为true,则使整个表达式为true.同样,如果next'N',则next!='n'再次为true,则使整个表达式为true.

If next is 'n' then next!='N' is true making the whole expression true. Similarly, if next is 'N' then next!='n' is true again making the whole expression true.

您要在此处使用逻辑与&&:

What you want here is to use a logical AND &&:

} while (next !='n' && next!='N')

这篇关于读取用户输入时循环不会停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆