循环条件不工作时C++

循环条件不工作时C++,c++,if-statement,nested-loops,do-while,nested-if,C++,If Statement,Nested Loops,Do While,Nested If,即使我输入CHOICE作为“N”或“N”,它也不会停止循环。 即使我输入“Y”或“Y”,它也不会询问我想要多少次尝试。 相反,它只是直接询问我想要输入什么整数。 请尝试复制和编译代码,以进一步了解问题所在。请帮忙 顺便说一句,这是我正在制作的一个猜测程序…如果用户输入的值不是其他值,则内部循环中的错误会被卡住,否则您不会尝试递减: #include<iostream> #include<ctime> #include<cstdlib> using names

即使我输入CHOICE作为“N”或“N”,它也不会停止循环。 即使我输入“Y”或“Y”,它也不会询问我想要多少次尝试。 相反,它只是直接询问我想要输入什么整数。 请尝试复制和编译代码,以进一步了解问题所在。请帮忙


顺便说一句,这是我正在制作的一个猜测程序…

如果用户输入的值不是其他值,则内部循环中的错误会被卡住,否则您不会尝试递减:

#include<iostream>
#include<ctime>
#include<cstdlib>

using namespace std;

int main(){
int gnumber, rnumber;
    char choice;
    int tries;

    do {
    cout << "Welcome to the Number Guessing Game!" << endl;
    cout << endl; // breakline
    cout << "How many tries: ";
    cin >> tries;
    cout << endl;

    while (tries > 0){

    srand(time(NULL));
    rnumber = rand() % 99;

    cout <<"Enter an integer greater than or equal to 0 and less than 100:";
    cin >> gnumber;
    system("cls");

    if (tries != 1){
        if (gnumber < 100 && gnumber >= 0){

            if (gnumber == rnumber){
            cout << "Congratulations! You've guessed the number." << endl;
            tries--;
            cout << "Remaining tries: " << tries << endl;   
            }

            else if (gnumber > rnumber){
                cout << "Your guess is higher than the number." << endl;
                tries--;
                cout << " Guess Again!" << endl;
                cout << "Remaining tries: " << tries << endl;
            }
            else{
                cout << "Your guess is lower than the number." << endl;
                tries--;
                cout << " Guess Again!" << endl;
                cout << "Remaining tries: " << tries << endl;
            }
        }
        else
        cout << "Must greater or equal to 0 and lesser than 100!" << endl;
    }
    else 
    {
        cout << "Game over!" <<" The number is: " << rnumber << endl;
        cout << "Play Again? (Y/N)" << endl;
        cin >> choice;
        system("cls");

    }

    }
    }while(choice == 'Y' || choice == 'y'); //  

    system("pause");
    return 0;
    }

您是否尝试过使用调试器单步执行代码?如果显示的代码中没有任何内容可以响应Y、Y、N或N,那么显示的代码执行任何操作都将是一个非常好的技巧。你的问题是你的电脑只做你让它做的事,而不是你想要它做什么。实际上它是“Y”或“Y”而不是“Y”或“Y”,因为choice被声明为char,所以“Y”或“Y”等于字母Y或Y。即使你输入了Y或Y作为choice,它也会结束程序。它会问在初始尝试次数变为0后有多少次再次尝试吗?
    else 
    {
        cout << "Game over!" <<" The number is: " << rnumber << endl;
        cout << "Play Again? (Y/N)" << endl;
        cin >> choice;
        system("cls");
        tries--; // add this
    }