本帖最後由 陳品肇 於 2022-2-26 11:30 編輯
以陣列存放 "剪刀" "石頭" "布", 改寫上一個程式.- #include<iostream>
- #include<cstdlib>
- #include<ctime>
- using namespace std;
- int main()
- {
- int player, computer;
- string meth[3]= {"剪刀", "石頭", "布"};
- cout<<"請出拳! (1)"+meth[0]+" (2)"+meth[1]+" (3)"+meth[2]+": ";
- cin>>player;
- cout<<"你出"+meth[player-1]+"!"<<endl;
- srand(time(NULL));
- // 產出 1~3 亂數
- computer = rand()%3+1;
- cout<<"電腦"+meth[computer-1]+"!"<<endl;
- system("pause");
- return 0;
- }
複製代碼 |