循环无休止地做

do while Loop Endless

本文关键字:无休止 循环      更新时间:2023-10-16

尝试弄清楚为什么选择y时这将是无尽的循环。当用户选择y或y时,循环时不应该离开do吗?

#include <iostream>
using namespace std;
int main()
{
    int choice;

    do {
        cout << "Enter a number. If you would like to quit, press y.";
        cin >> choice;
        if (choice % 2 == 1)
        {
            cout << "Odd n";
        }
        else
        {
            cout << "Even n";
        }
    } while (choice != 'y'|| choice != 'Y');

    return 0;
}

choiceint;您无法将字母y读入其中。尝试时,它会"破裂" cin,导致您的无尽环路。