返回列表 發帖

猜拳遊戲 (二)

本帖最後由 tonyh 於 2020-1-3 14:39 編輯

以陣列存放 "剪刀" "石頭" "布",改寫上一個程式。
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     srand(time(NULL));
  7.     int player,computer;
  8.     string n[]={"剪刀","石頭","布"};
  9.     cout<<"請出拳! (1)剪刀(2)石頭(3)布 ";
  10.     cin>>player;
  11.     computer=rand()%3+1;   //1~3
  12.     cout<<"你出"<<n[player-1]<<endl;
  13.     cout<<"電腦出"<<n[computer-1]<<endl;
  14.     system("pause");   
  15.     return 0;
  16. }
複製代碼

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     srand(time(NULL));
  7.     int a,b;
  8.     string n[]={"剪刀","石頭","布"};
  9.     cout<<"請出拳! (1)剪刀(2)石頭(3)布 ";
  10.     cin>>a;
  11.     b=rand()%3+1;
  12.     cout<<"你出";
  13.     cout<<n[a-1]<<endl;
  14.     cout<<"電腦出";
  15.     cout<<n[b-1]<<endl;
  16.     system("pause");   
  17.     return 0;
  18. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     srand(time(NULL));
  7.     int player,computer;
  8.     string yee[]={"剪刀","石頭","布 "};
  9.     cout<<"請出拳! (1)剪刀(2)石頭(3)布 ";
  10.     cin>>player;
  11.     computer=rand()%3+1;
  12.     cout<<"你出"<<yee[player-1]<<endl;
  13.     cout<<"電腦出"<<yee[computer-1]<<endl;
  14.     system("pause");   
  15.     return 0;
  16. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {   
  6.     int x, y;
  7.     srand(time(NULL));
  8.     string n[]={"剪刀","石頭","布"};
  9.     cout<<"請出拳! <1>剪刀 <2>石頭 <3>布 ";
  10.     cin>>x;   
  11.     y=rand()%3+1;
  12.     cout<<"你出"<<n[x-1]<<endl;
  13.     cout<<"電腦出"<<n[y-1]<<endl;
  14.      
  15.     system("pause");   
  16.     return 0;
  17. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     srand(time(NULL));
  7.     int player,computer;
  8.     string n[]={"剪刀","石頭","布"};
  9.     cout<<"請出拳! (1)剪刀(2)石頭(3)布 ";
  10.     cin>>player;
  11.     computer=rand()%3+1;   //1~3
  12.     cout<<"你出"<<n[player-1]<<endl;
  13.     cout<<"電腦出"<<n[computer-1]<<endl;
  14.     system("pause");   
  15.     return 0;
  16. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     srand(time(NULL));
  7.     int player,computer;
  8.     string n[]={"剪刀","石頭","布"};
  9.     cout<<"請出拳! (1)剪刀(2)石頭(3)布 ";
  10.     cin>>player;
  11.     computer=rand()%3+1;   //1~3
  12.     cout<<"你出"<<n[player-1]<<endl;
  13.     cout<<"電腦出"<<n[computer-1]<<endl;
  14.     system("pause");   
  15.     return 0;
  16. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     srand(time(NULL));
  7.     int player,computer;
  8.     string n[]={"剪刀","石頭","布"};
  9.     cout<<"請出拳! (1)剪刀(2)石頭(3)布 ";
  10.     cin>>player;
  11.     computer=rand()%3+1;   //1~3
  12.     cout<<"你出"<<n[player-1]<<endl;
  13.     cout<<"電腦出"<<n[computer-1]<<endl;
  14.     system("pause");   
  15.     return 0;
  16. }
複製代碼

TOP

返回列表