本帖最後由 葉桔良 於 2022-7-30 16:36 編輯
延續 猜拳遊戲 (二) 的練習,
完成: 1. 判斷輸贏 2. 加上 goto 敘述使可重複遊戲
- #include<iostream>
- #include<cstdlib>
- #include<ctime>
- using namespace std;
- int main()
- {
-
- re:
- srand(time(NULL));
- int player,computer;
- string n[]={"剪刀","石頭","布"};
- cout<<"請出拳! (1)剪刀 (2)石頭 (3)布 ";
- cin>>player;
- computer=rand()%3+1;
- cout<<"你出"<<n[player-1]<<endl;
- cout<<"電腦出"<<n[computer-1]<<endl;
- if(player==computer)
- cout<<"平手!"<<endl;
- else if((player==1 && computer==3)||(player==2 && computer==1)||(player==3 && computer==2))
- cout<<"你贏了!"<<endl;
- else
- cout<<"電腦贏了!"<<endl;
- cout<<endl;
- goto re;
- system("pause");
- return 0;
- }
複製代碼 |