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

前端 未结 7 1904
伪装坚强ぢ
伪装坚强ぢ 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:24

    I would recommend multi-character character constants to achieve this in a succinct manner:

    switch ((play1 << 8) + play2) {
    
      case 'RP':
    
        printf ("Paper wins!");
        break;
    
      case 'RS':
    
        printf ("Rock wins!");
        break;
    
      case 'RR':
    
        printf ("Draw!");
        break;
     }
    

提交回复
热议问题