因为它早些时候工作得很好,所以我抓耳挠腮,但是当我去添加一些其他功能时,我的程序突然崩溃了,我无法让它恢复到原来的状态。
类(class)让我编写了一个石头/剪刀布程序来对抗计算机,任何关于为什么循环不断终止自身的帮助都会很棒
#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;
}
最佳答案
if (choice=='Q'||'q')
这相当于
if ((choice == 'Q') || 'q')
这几乎肯定不是您想要的。 'q'
是一个非零的 char
文字,它是“真实的”,因此这个表达式永远不会为假。这类似于编写 if (choice == 'Q' || true)
。
解决方法是:
if (choice=='Q' || choice=='q')
关于c++ While循环终止与功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43532808/