c ++ While循环终止功能

Anc*_*bis 4 c++ loops while-loop srand switch-statement

因为它在早些时候工作得很好,但是当我去添加一些其他功能时,我的程序吓坏了,我无法将它恢复到原来的状态.

我让我写一个摇滚/纸/剪刀程序来对抗计算机,任何有关循环不断终止的帮助都会很精彩

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;


void RPSout(char);
int RPScomp();

int main() {

    char choice;
    int endit=0;

    while (endit == 0)
    {
        cout << "\n\n\tReady to play Rock/Paper/Scissors against the computer??(please choose R/P/S)(Q to quit)\n";
        cin >> choice;

        RPSout(choice);

        if (choice=='Q'||'q')
            {endit=1;}
    }
    return 0;
}


void RPSout(char choose)
{
    int RPS =0;
    int comp=0;
    switch (choose)
    {
    case 'R':
    case 'r':
    {
        cout <<"Your choice: Rock";
        break;
    }
    case 'P':
    case 'p':
    {
        cout <<"Your choice: Paper";
        break;
    }

    case 'S':
    case 's':
    {
        cout << "Your choice: Scissors";
        break;
    }

    case 'Q':
    case 'q':
    {
        cout << "Bye Bye Bye";
        break;
    }

    default:
        cout<<"You enter nothing!"<<endl;
        cout << "The valid choices are R/P/S/Q)";
    }
    return;
}

int RPScomp()
{
int comp=0;
const int MIN_VALUE =1;
const int MAX_VALUE =3;
    unsigned seed = time(0);

    srand(seed);

    comp =(rand() % (MAX_VALUE - MIN_VALUE +1)) + MIN_VALUE;
    return comp;
}
Run Code Online (Sandbox Code Playgroud)

cdh*_*wie 5

if (choice=='Q'||'q')
Run Code Online (Sandbox Code Playgroud)

这相当于

if ((choice == 'Q') || 'q')
Run Code Online (Sandbox Code Playgroud)

这几乎肯定不是你想要的. 'q'是一个非零的char文字,这是"真理",所以这个表达永远不会是假的.它类似于写作if (choice == 'Q' || true).

解决方案是:

if (choice=='Q' || choice=='q')
Run Code Online (Sandbox Code Playgroud)