(C ++)If - Else if CommandLine rock paper的语句剪刀游戏

时间:2014-04-22 06:58:39

标签: c++ if-statement

我对c ++很新,但一直在阅读很多文档,无法弄清楚这里发生了什么。我曾经使用过if / else if语句来处理其他事情,但也许这只是一个大脑放屁。我真的不知道。当我输入" 1"当它要求输入时我按下回车键直接进入Else声明


#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");
}

7 个答案:

答案 0 :(得分:2)

您尝试将charint进行比较,这不会按照您的意愿进行比较,将choice的类型更改为int }。

此外,没有名为p1的变量,可能应更改为r1

答案 1 :(得分:1)

'1','2'和'3'的字符与整数1,2和3不同。在ascii编码下,例如它们将是49,50和51。

您可以通过将输入的字符与实际字符进行比较来解决此问题,如下所示:

char r1 = '1';
char p2 = '2';
char s3 = '3';

答案 2 :(得分:1)

问题是cin >> choice正在提供字符,例如&#39; 1&#39 ;.角色&#39; 1&#39;存储为49的字节值,因此与整数1进行比较时,比较失败。

iow:&#39; 1&#39; == 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';
    ...
}

答案 3 :(得分:0)

更改声明int r1 = 1; to int pa = 1;。希望它会成功。

答案 4 :(得分:0)

如前所述,使用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;
            }
}}

答案 5 :(得分:0)

为什么要将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;
    }
}

答案 6 :(得分:0)

代码的问题是输入的对象值的类型是char。因此,当您键入例如1时,会将其输入为字符'1'。同时r1具有积分值1。 &#39; 1&#39;不等于1.当&#39; 1&#39;比较1然后比较字符的内部代码&#39; 1&#39;。对于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" );
}
编辑:我在上一个节目中更新了一些拼写错误。