#include<iostream>
#include<cstdlib>
#include<ctime>
#include<string>
using namespace std;
int main()
{
char replay;
int userInput;
cout<< "Let's play Rock, Paper, Scissors"<<endl;
do
{
cout<<"Enter 1 for Rock, 2 for Paper, 3 for Scissors"<< endl;
cin>> userInput;
switch(userInput)
{
case 1:
cout <<"You chose rock" << endl;
break;
case 2:
cout <<"You chose paper" <<endl;
break;
case 3:
cout <<"You chose scissors" << endl;
break;
default:
cout << userInput << " is not a valid choice"<< endl;
break;
}
cout<<"Would you like to play again (Y for yes, N for no)?"<<endl;
cin >> replay;
} while((replay=='Y') || (replay=='y'));
return 0;
}
當我在輸入數字的回答中輸入一個字符時,當我被問及是否要再次玩時,輸入的字符不是Y,Y,N或n時,進入無限循環爲什麼我的程序在輸入字符時會出現無限循環?
你在使用,因爲我不能複製VS2010下,即使是在HTTP編譯:// ideone .COM/C88qG3? – herohuyongtao
@herohuyongtao g ++和我在終端 – user2988803