[英](C++) If - Else if statements for a CommandLine rock paper scissors game
我對C ++相當陌生,但是一直在閱讀大量文檔,無法弄清這里發生了什么。 我已經用過if / else if聲明了其他事情,但是也許這只是腦子屁。 我真的不知道。 當我要求輸入時鍵入“ 1”時,我按Enter鍵,它直接進入其他語句
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
char choice;
int p2 = 2;
int r1 = 1;
int s3 = 3;
//questions
cout << "Lets Play Rock Paper Scissors" <<endl;
cout << "(Use Letter) Rock (1) - Paper (2) - Scissors (3)" <<endl;
que:
cout << "What is your choice? : ";
cin >> choice;
if (choice == p1){
cout << "You choose Rock" <<endl;}
else if (choice == p2){
cout << "You choose Paper" <<endl;}
else if (choice == s3){
cout << "You choose Scissors" <<endl;}
else
goto que;
system("PAUSE");
}
您正在嘗試將char
與int
進行比較,這將無法按您希望的方式工作,請將choice
類型更改為int
。
此外,沒有變量p1
,應該將其更改為r1
。
“ 1”,“ 2”和“ 3”的字符與整數1、2和3不同。例如,在ascii編碼下,它們分別為49、50和51。
您可以通過將輸入的字符與實際字符進行比較來解決此問題,如下所示:
char r1 = '1';
char p2 = '2';
char s3 = '3';
問題在於cin >> choice
給出了字符,例如'1'。 字符“ 1”存儲為字節值49,因此當與整數1比較時,比較失敗。
低:'1'== 1等效於49 == 1,這是錯誤的。
有兩種方法可以解決此問題。
1)將選擇類型更改為int:
int main()
{
int choice;
int p2 = 2;
int r1 = 1;
int s3 = 3;
...
}
或2)將p2,r1,s3的類型更改為char:
int main()
{
char choice;
char p2 = '2';
char r1 = '1';
char s3 = '3';
...
}
更改聲明int r1 = 1; 到int pa = 1;。 希望它能解決。
如前所述,將int用作類型。 我也不願意使用goto,因為這是不好的做法。 此外,這似乎是切換的完美工作。 這是一些修改后的代碼。
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
int choice;
int p2 = 2;
int r1 = 1;
int s3 = 3;
//questions
cout << "Lets Play Rock Paper Scissors" <<endl;
for(;;) {
cout << "(Use Letter) Rock (1) - Paper (2) - Scissors (3)" <<endl;
cout << "What is your choice? : ";
cin >> choice;
switch (choice) {
case 1: cout << "You choose Rock" <<endl; break;
case 2: cout << "You choose Rock" <<endl; break;
case 3: cout << "You choose Rock" <<endl; break;
default: cout << "Unknown Option" << endl; break;
}
}}
為什么要比較int和char? 另外,如果這就是您的全部代碼,則不使用字符串,cstdlib和sstream庫...
#include <iostream>
using namespace std;
int main()
{
char choice;
char rock = '1';
char paper = '2';
char scissors = '3';
cout << "Let's play rock paper scissors\n";
cout << "Rock = 1, Paper = 2, Scissors = 3\n";
que:
cout << "What is your choice? : ";
cin >> choice;
if (choice == rock)
{
cout << "You picked Rock!";
}
else if (choice == paper)
{
cout << "You picked Paper!";
}
else if (choice == scissors)
{
cout << "You picked Scissors!";
}
else
{
goto que;
}
}
代碼的問題在於,輸入對象值的類型是char
。 因此,當您鍵入示例1
它將輸入為字符'1'
。 同時r1的整數值為1。“ 1”不等於1。當將“ 1”與1進行比較時,將比較字符“ 1”的內部代碼。 對於ASCII,其值為49。對於EBCDIC,其值為241。您可以編寫例如
choice -= '0';
if ( choice == r1 )
{
cout << "You choose Rock" <<endl;
}
//...
你可以寫一個測試程序
#include <iostream>
int main()
{
char c = '1';
int i = 1;
if ( c == i ) std::cout << ( int )c << " is equal to " << i << std::endl;
else std::cout << ( int )c << " is not equal to " << i << std::endl;
}
希望你能得到結果:)
49 is not equal to 1
還要考慮到您的代碼有錯字。 我認為這句話
if (choice == p1){
cout << "You choose Rock" <<endl;}
必須有r1
而不是p1
if (choice == r1){
cout << "You choose Rock" <<endl;}
使用goto語句也是一個壞主意。
該程序可能看起來像
#include <cstdlib>
#include <iostream>
int main()
{
const char *name[] = { "Rock", "Paper", "Scissors" };
enum { Rock, Paper, Scissors };
char choice;
//questions
std::cout << "Lets Play Rock Paper Scissors" << std::endl;
std::cout << "(Use Letter) " << name[Rock] << " (" << Rock + 1 << ") - "
<< name[Paper] << " (" << Paper + 1 << ") - "
<< name[Scissors] << " (" << Scissors + 1 << ")" << std::endl;
do
{
std::cout << "What is your choice? : ";
std::cin >> choice;
switch ( choice - '0' - 1 )
{
case Rock:
std::cout << "You choose " << name[Rock] << std::endl;
break;
case Paper:
std::cout << "You choose " << name[Paper] << std::endl;
break;
case Scissors:
std::cout << "You choose " << name[Scissors] << std::endl;
break;
default:
break;
}
} while ( choice < '1' || choice > '3' );
std::system( "PAUSE" );
}
編輯:我在上一個程序中更新了一些錯字。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.