本帖最後由 周政輝 於 2017-9-9 10:14 編輯
新增 我方與電腦 出拳的結果- #include<iostream>
- #include<cstdlib>
- #include<time.h>
- using namespace std;
- int main()
- {
- // 1 剪刀 2 石頭 3 布
- // 使用者可以自行選擇要出哪一個,電腦自動出拳
- // 判斷最後結果誰贏
- // 隨機 1~3
-
- int player =0;
- int computer = 0;
-
- cout << "請出拳 輸入 1 剪刀 2 石頭 3 布" << endl;
- cin >> player;
- string data [] = {"剪刀","石頭","布"};
- srand(time(NULL));
- computer = (rand()%3)+1;
-
- cout << "你出 :" << data[player-1]<<endl;
- cout << "電腦出 :" << data[computer-1]<<endl;
- cout << "結果: ";
-
- if(player ==computer)
- {
- cout << "平手" << endl;
- }
-
- else if(player ==1 && computer == 3)
- {
- cout << "你贏了" << endl;
- }
-
- else if(player ==2 && computer == 1)
- {
- cout << "你贏了" << endl;
- }
-
-
- else if(player ==3 && computer == 2)
- {
- cout << "你贏了"<<endl;
- }
-
- else
- {
- cout << "你輸了" << endl;
- }
-
-
- system("pause");
- return 0;
- }
複製代碼 |