模拟java_小猿圈分享使用Java模拟三道门的游戏

美国以前有一个综艺节目。参赛者会看见三扇关闭了的门,其中一扇的后面有一辆汽车,选中后面有车的那扇门可赢得该汽车,另外两扇门后面则各藏有一只山羊。当参赛者选定了一扇门,但未去开启它的时候,节目主持人开启剩下两扇门的其中一扇,露出其中一只山羊。主持人其后会问参赛者要不要换另一扇仍然关上的门。问题是:换另一扇门会否增加参赛者赢得汽车的机会率?

以下为模拟三门问题的Java代码。

import java.util.Scanner;

import java.text.DecimalFormat;

import java.text.DecimalFormatSymbols;

//三门

class ThreeDoor {

String[] threeDoor = new String[3]; //三门

//设置门后的奖品

public void setPrize() {

int carPosition = (int)(Math.random()*100)%3;

threeDoor[0] = "Goat";

threeDoor[1] = "Goat";

threeDoor[2] = "Goat";

threeDoor[carPosition] = "Car";

}

}

//玩家

class Player {

int firstChoicePosition = 0; //首次选择的门

int lastChoicePosition = 0; //最终选择的门

//选择一个门

public void choiceDoor() {

firstChoicePosition = (int)(Math.random()*100)%3;

lastChoicePosition = firstChoicePosition;

}

//更换为另一个门

public void changeDoor(int firstGoatPosition) {

lastChoicePosition = 3 - firstChoicePosition - firstGoatPosition;

}

}

//主持人

class Presenter {

int firstGoatPosition = 0; //主持人打开的门

//打开后面为山羊的门

public void openFirstGoatPosition (String[] threeDoor,int playerFirstChoicePosition) {

if(threeDoor[playerFirstChoicePosition].equals("Car")) {

do {

firstGoatPosition = (int)(Math.random()*100)%3;

} while (firstGoatPosition == playerFirstChoicePosition);

} else {

for(int i=0;i<3;i++)

if(!threeDoor[i].equals("Car") && i!=playerFirstChoicePosition)

firstGoatPosition = i;

}

}

}

//计分牌

class Scorer {

int playCount = 0; //玩的总次数

int choiceCarCount = 0; //获得车的次数

int choiceGoatCount = 0; //获得山羊的次数

//计分

public void score(String[] threeDoor,int playLastChoicePosition) {

playCount++;

if(threeDoor[playLastChoicePosition].equals("Car"))

choiceCarCount++;

else

choiceGoatCount++;

}

//计算获得车的概率

public void statistics() {

DecimalFormat df = new DecimalFormat("##.00%");

System.out.println("Choice Goat Count: " + choiceGoatCount);

System.out.println("Choice Car Count: " + choiceCarCount);

System.out.println("Choice Car Rate : " + df.format((float)choiceCarCount/playCount));

}

}

//设置模式

class Moder {

String mode = ""; //模式,A:换门,B:不换门

int playTotalCount = 0; //玩的总次数

//设置模式

public void setMode() {

Scanner sc = new Scanner(System.in);

//设置选门模式

while(!mode.equals("A") && !mode.equals("B")) {

System.out.println("Plase input mode: A[Change] B[Don't Change]");

mode = sc.nextLine();

if(!mode.equals("A") && !mode.equals("B"))

System.out.println("Input Error, Input again.");

}

//设置玩的总次数

while(playTotalCount<=0) {

System.out.println("Plase input play total count: ");

try {

Scanner scCnt = new Scanner(System.in);

playTotalCount = scCnt.nextInt();

} catch(Exception e) {

playTotalCount = 0;

}

if(playTotalCount<=0)

System.out.println("Input Error, Input again.");

}

System.out.println();

}

//显示设置的模式

public void showMode() {

if(mode.equals("A"))

System.out.println("Mode: [Change]");

else

System.out.println("Mode: [Don't Change]");

System.out.println("Play Total Count: " + playTotalCount);

System.out.println();

}

}

//main程序

class ThreeDoorDemo {

public static void main(String[] args) {

ThreeDoor threeDoor = new ThreeDoor(); //三门

Player player = new Player(); //玩家

Presenter presenter = new Presenter(); //主持人

Scorer scorer = new Scorer(); //计分者

Moder moder = new Moder(); //设置模式者

moder.setMode(); //设置模式

moder.showMode(); //显示模式

//循环玩多次

for(int i=0; i

threeDoor.setPrize(); //设置门后奖品

player.choiceDoor(); //玩家选择一个门

//主持人打开一扇是山羊的门

presenter.openFirstGoatPosition(threeDoor.threeDoor,player.firstChoicePosition);

if(moder.mode.equals("A"))

player.changeDoor(presenter.firstGoatPosition); //玩家换另外一扇门

//计分

scorer.score(threeDoor.threeDoor,player.lastChoicePosition);

}

scorer.statistics(); //统计获得汽车的概率

}

}

好啦,今天小猿圈关于Java学习的分享就到这里了,大家是不是感觉很神奇呢,原来这个游戏套路这么深,希望大家能够学习到呢,记得收藏分享给身边的人哦。

80f277f9d960597c75b38a13432d8aa4.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值