篇一:剪刀石头布编程
C++编程剪刀石头布
#includeiostream
#includecstdlib
#includectime
using namespace std;
class Player{ //定义表示玩家的类
private:
int game; //使用game保存出拳的值
public:
Player(int g=0):game(g){};//构造函数
void getGame(){
cout请选择:0—石头 1-剪刀 2-布;
cingame;
}
void autoGame(){
game=rand()%3;//自动出拳
}
void judge(Player c){ // 裁决胜负
const int sf={{0,1,-1},{-1,0,1},{1,-1,0}};
cout玩家:(game==0?石头:c.game==1?剪子:布);
cout-电脑:(c.game==0?石头:c.game==1?剪子:布)endl;if(sf==0) cout平局;
if(sf==1) cout恭喜!你赢了;
if(sf==-1) cout哈哈,你输了;
}
};
int main()
{ Player person, computer;//创建两个玩家:person和computer
srand(time(0));//;利用当前时间,初始化随机数序列
person.getGame();//玩家person使用键盘出拳
computer.autoGame();//玩家computer自动出拳
person.judge(computer);//评判玩家person和computer的胜负
return 0;
}
篇二:剪刀石头布与概率
“剪刀、石头、布” 与概率
文/阳光心语
同学们一定玩过“剪刀、石头、布”的游戏吧,知道吗?在这个游戏中还藏着数学问题呢。我们一起来看下面这个问题:A、B两人用石头、剪子、布来做某种决定,求一共有多少种可能的结果?A获胜的可能性是多少?
分析与解:要求一共有多少种结果,我们可以用列举法把A和B猜拳的组合一一列举出来: (剪刀,剪刀)、(剪刀,石头)、(剪刀,布);(石头,剪刀)、(石头,石头)、(石头,布);(布,剪刀)、(布,石头)、(布,布)共9种,所以一共有九种可能的结果。 在这9种组合中,A获胜的组合一共有(剪刀,布)、(石头,剪刀)、(布,石头)3种,那么A获胜的概率就是3/9=1/3。
篇三:JAVA 编程剪刀石头布
import java.util.*;
class Game1{
public static void main(String args){
System.out.println(欢迎来到剪刀石头布游戏);People p=new People();
Computer c=new Computer();
Referee r=new Referee();
r.games(p,c);
}
}
class People{
public int chuQuan(){
Scanner sc=new Scanner(System.in);
System.out.println(请出拳);
int a=sc.nextInt();
return a;
}
}
class Computer{
public int chuQuan(){
int a=new Random().nextInt(3);
return a;
}
}
class Referee{
public void games(People p1,Computer c1){int a=p1.chuQuan();
int b=c1.chuQuan();
switch(a){
case 0:
System.out.println(你出的是石头); break;
case 1:
System.out.println(你出的是布); break;
default:
System.out.println(你出的是剪刀);
} /