【问题标题】:why does the do while loop doesn't break?为什么 do while 循环不会中断?
【发布时间】:2021-04-14 23:21:49
【问题描述】:

下面的代码有问题

switch(adminChoice){
    case 1:
        do{
            cout<<"\nEnter Student ID: ";
            cin>>studentId;
            cout<<"\nEnter Student Name: ";
            cin>>studentName;
            cout<<"\nEnter Student Password: ";
            cin>>studentPassword;
            cout<<"\nAdd another account? y/n ";
            cin>>adminSubChoice;
        }
        while(adminSubChoice!='n' || adminSubChoice !='N');
        break;
    case 2:

当我为 char 变量 adminSubChoice 输入 n 或 N 时,do while 不会中断,它会一次又一次地运行。谁能告诉我我做错了什么??

【问题讨论】:

  • 谢谢老兄。我得到了答案,并且在问题 adminSubChoice is char type variable 中也提到了它。

标签: c++ loops data-structures switch-statement do-while


【解决方案1】:

条件adminSubChoice!='n' || adminSubChoice !='N' 将始终为真,因为没有任何字符会同时是nN

条件应该是adminSubChoice!='n' &amp;&amp; adminSubChoice !='N'

【讨论】:

  • 作为此答案的后续,请考虑阅读德摩根定律,因为它们似乎是您想要弄清楚的:en.wikipedia.org/wiki/De_Morgan%27s_laws
  • @MikeCAT 成功了。我现在明白了。谢谢
  • @Welbog 是的,我对 OR & AND 运算符感到困惑。谢谢,我去看看
猜你喜欢
  • 2016-05-26
  • 1970-01-01
  • 1970-01-01
  • 2022-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多