返回列表 發帖

猜拳遊戲 (二)

本帖最後由 陳品肇 於 2022-2-26 11:30 編輯

以陣列存放 "剪刀" "石頭" "布", 改寫上一個程式.
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<ctime>
  4. using namespace std;
  5. int main()
  6. {
  7.     int player, computer;
  8.     string meth[3]= {"剪刀", "石頭", "布"};
  9.     cout<<"請出拳! (1)"+meth[0]+" (2)"+meth[1]+" (3)"+meth[2]+": ";

  10.     cin>>player;
  11.     cout<<"你出"+meth[player-1]+"!"<<endl;
  12.     srand(time(NULL));
  13.     // 產出 1~3 亂數
  14.     computer = rand()%3+1;   
  15.     cout<<"電腦"+meth[computer-1]+"!"<<endl;
  16.     system("pause");
  17.     return 0;
  18. }
複製代碼

返回列表