標題:
[隨堂測驗] 猜拳遊戲 (三)
[打印本頁]
作者:
方浩葦
時間:
2024-7-19 21:00
標題:
[隨堂測驗] 猜拳遊戲 (三)
延續
猜拳遊戲 (二)
的練習,
完成: 1. 判斷輸贏 2. 加上 goto 敘述使可重複遊戲
本帖隱藏的內容需要回復才可以瀏覽
作者:
林少謙
時間:
2024-7-20 15:44
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
re:
srand(time(NULL));
int p,c;
string a[]{"剪刀","石頭","布"};
cout<<"請出拳! (1)剪刀(2)石頭(3)布 ";
cin>>p;
c=rand()%3+1;
cout<<"你出"<<a[p-1]<<endl;
cout<<"電腦出"<<a[c-1]<<endl;
if(p==c)
{
cout<<"平手"<<endl<<endl;
}
else if(p==1&&c==2)
{
cout<<"電腦贏了"<<endl<<endl;
}
else if(p==2&&c==3)
{
cout<<"電腦贏了"<<endl<<endl;
}
else if(p==1&&c==3)
{
cout<<"你贏了"<<endl<<endl;
}
else if(p==2&&c==1)
{
cout<<"你贏了"<<endl<<endl;
}
else if(p==3&&c==2)
{
cout<<"你贏了"<<endl<<endl;
}
else if(p==3&&c==1)
{
cout<<"電腦贏了"<<endl<<endl;
}
else
cout<<"error"<<endl<<endl;
goto re;
system("pause");
return 0;
}
複製代碼
作者:
劉奕劭
時間:
2024-7-20 15:58
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
srand(time(NULL));
int p,c;
string a[]={"剪刀","石頭","布"};
re:
cout<<"請出拳! (1)剪刀(2)石頭(3)布 ";
cin>>p;
c=rand()%3+1;
cout<<"你出"<<a[p-1]<<endl;
cout<<"電腦出"<<a[c-1]<<endl;
if(p==1&&c==2)
{
cout<<"電腦贏了"<<endl;
}
else if(p==2&&c==3)
{
cout<<"電腦贏了"<<endl;
}
else if(p==1&&c==3)
{
cout<<"你贏了"<<endl;
}
else if(p==2&&c==1)
{
cout<<"你贏了"<<endl;
}
else if(p==3&&c==2)
{
cout<<"你贏了"<<endl;
}
else if(p==3&&c==1)
{
cout<<"電腦贏了"<<endl;
}
else
{
cout<<"平手"<<endl;
}
goto re;
system("pause");
return 0;
}
複製代碼
作者:
陳妍蓁
時間:
2024-7-20 16:11
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main(){
re:
srand(time(NULL));
string n[]={"剪刀","石頭","布"};
int computer,player;
cout<<"請出拳 <1>剪刀 <2>石頭 <3>布"<<endl;
cin>>player;
computer=rand()%3+1;
cout<<"你出"<<n[player-1]<<endl;
cout<<"電腦出"<<n[computer-1]<<endl;;
if(computer==player)
cout<<"平手"<<endl;
else if(computer==1 && player==2)
cout<<"你贏了"<<endl;
else if(computer==2 && player==3)
cout<<"你贏了"<<endl;
else if(computer==3 && player==1)
cout<<"你贏了"<<endl;
else if(computer==1 && player==3)
cout<<"你輸了"<<endl;
else if(computer==2 && player==1)
cout<<"你輸了"<<endl;
else if(computer==3 && player==2)
cout<<"你輸了"<<endl;
system("pause");
goto re;
return 0;
}
複製代碼
作者:
洪榮辰
時間:
2024-7-20 16:19
本帖最後由 洪榮辰 於 2024-7-20 16:24 編輯
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
re:
srand(time(NULL));
int p, c;
string n[]={"剪刀","石頭","布"};
cout<<"請出拳! (1)剪刀(2)石頭(3)布 ";
cin>>p;
c=rand()%3+1;
cout<<"你出"<<n[p-1]<<"!"<<endl;
cout<<"電腦出"<<n[c-1]<<"!"<<endl;
if(p==c)
{cout<<"平手!"<<endl;}
else if(p==1 && c==2)
{cout<<"你輸了!"<<endl;}
else if(p==1 && c==3)
{cout<<"你贏了!"<<endl;}
else if(p==2 && c==1)
{cout<<"你贏了!"<<endl;}
else if(p==2 && c==3)
{cout<<"你輸了!"<<endl;}
else if(p==3 && c==1)
{cout<<"你輸了!"<<endl;}
else if(p==3 && c==2)
{cout<<"你贏了!"<<endl;}
cout<<endl;
goto re;
system("pause");
return 0;
}
複製代碼
作者:
李偈睿
時間:
2024-7-20 16:29
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
re:
system("cls");
srand(time(NULL));
int player,pc;
string n[]={"剪刀","石頭","布"};
cout<<"請出拳! (1)剪刀(2)石頭(3)布 ";
cin>>player;
pc=rand()%3+1;
cout<<"你出"<<n[player-1]<<endl;
cout<<"電腦出"<<n[pc-1]<<endl;
if(player==pc)
{
cout<<"平手"<<endl;
}
else if(player==1 && pc==2)
{
cout<<"電腦贏了"<<endl;
}
else if(player==2 && pc==3)
{
cout<<"電腦贏了"<<endl;
}
else if(player==3 && pc==1)
{
cout<<"電腦贏了"<<endl;
}
else if(player==2 && pc==1)
{
cout<<"你贏了"<<endl;
}
else if(player==3 && pc==2)
{
cout<<"你贏了"<<endl;
}
else if(player==1 && pc==3)
{
cout<<"你贏了"<<endl;
}
system("pause");
goto re;
return 0;
}
複製代碼
作者:
李唯銘
時間:
2024-7-26 20:36
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
re:
srand(time(NULL));
int p,c;
string a[]{"剪刀","石頭","布"};
cout<<"請出拳! (1)剪刀(2)石頭(3)布 ";
cin>>p;
c=rand()%3+1;
cout<<"你出"<<a[p-1]<<endl;
cout<<"電腦出"<<a[c-1]<<endl;
if(p==c)
{
cout<<"平手"<<endl<<endl;
}
else if(p==1&&c==2)
{
cout<<"電腦贏了"<<endl<<endl;
}
else if(p==2&&c==3)
{
cout<<"電腦贏了"<<endl<<endl;
}
else if(p==1&&c==3)
{
cout<<"你贏了"<<endl<<endl;
}
else if(p==2&&c==1)
{
cout<<"你贏了"<<endl<<endl;
}
else if(p==3&&c==2)
{
cout<<"你贏了"<<endl<<endl;
}
else if(p==3&&c==1)
{
cout<<"電腦贏了"<<endl<<endl;
}
else
cout<<"error"<<endl<<endl;
goto re;
system("pause");
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2