How do I transform an IF statement with 2 variables onto a switch function using C?

前端 未结 7 1906
伪装坚强ぢ
伪装坚强ぢ 2021-01-27 07:49

I have an IF-statement that I want to transform into a Switch-statement... But it has 2 variables! Is it possible to do it on C?

It is a rock, paper, scissors game:

7条回答
  •  孤独总比滥情好
    2021-01-27 08:11

    #include 
    #include 
    
    #define PAIR(X,Y) (X<<8)|Y
    
    int main()
    {
        char play1, play2;
    
        printf("\nPlayer 1 - Enter your Play: ");
        scanf ("%c", &play1);
        printf("\nPlayer 2 - Enter your play: ");
        scanf (" %c", &play2); 
    
        switch (PAIR(play1, play2)) {
            case PAIR('R','P'):
                printf ("Paper wins!\n");
                break;
            case PAIR('R','S'):
                printf ("Rock wins!\n");
                break;
            case PAIR('R','R'):
                printf ("Draw!\n");
                break;
            default: //any thing else
                printf ("Default!\n");
                break;
        }
    }
    

提交回复
热议问题