首页 > 试题广场 > 编程模拟剪刀、石头和布游戏。游戏规则为:剪刀剪纸,石头砸剪刀
[问答题]

编程模拟剪刀、石头和布游戏。游戏规则为:剪刀剪纸,石头砸剪刀,布包石头。玩游戏者从键盘输入s(表示剪刀)或r(表示石头)或p(表示布),要求两个游戏者交替输入,计算机给出输赢的信息。

推荐
#include<iostream>
using namespace std;
int main()
{
char first,second;
  cout << "First input( s,r or p ):";
  cin >> first;
  cout << "Second input( s,r or p ):";
  cin >> second;
  switch  ( first )
  {
case  's':
 switch  ( second )
{
case  's': cout << "Scissor ties scissor." << endl; goto end;   
case  'r': cout << "Scissor is crushed by rock." << endl; goto end;
case  'p': cout << "Scissor cuts paper." << endl; goto end;                   
default :  cout << "second input error!" << endl ; goto end;
}
    case  'r':
switch  ( second )
    {
case  's': cout << "Rock crushes scissor." << endl; goto end;   
      case  'r': cout << "Rock ties rock." << endl; goto end;
      case  'p': cout << "Rock is wrapped by paper." << endl; goto end;  
      default :  cout << "second input error!" << endl; goto end;
    }
    case  'p':
    switch  ( second )
{
case  's': cout << "Paper is cut by scissor." << endl; goto end;   
case  'r': cout << "Paper wraps the rock." << endl; goto end;
  case  'p': cout << "Paper ties paper." << endl; goto end;  
default :  cout << "second input error!" << endl; goto end;
}
    default : cout << "First input error!" << endl; goto end;
  }
end: ;
}

发表于 2018-05-07 11:15:24 回复(0)