美文网首页
小猿圈分享使用Java模拟三道门的游戏

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

作者: 小猿圈IT教育 | 来源:发表于2019-05-31 09:48 被阅读0次

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

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

    importjava.util.Scanner;

    importjava.text.DecimalFormat;

    importjava.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 {

    intfirstChoicePosition = 0; //首次选择的门

    intlastChoicePosition = 0;  //最终选择的门

    //选择一个门

    public void choiceDoor() {

        firstChoicePosition = (

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

        lastChoicePosition =firstChoicePosition;

      }

    //更换为另一个门

    public void changeDoor(intfirstGoatPosition) {

        lastChoicePosition =

    3-firstChoicePosition - firstGoatPosition;

      }

    }

    //主持人class Presenter {

    intfirstGoatPosition = 0;  //主持人打开的门

    //打开后面为山羊的门

    public void openFirstGoatPosition (String[]

    threeDoor,intplayerFirstChoicePosition) {

    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,intplayLastChoicePosition) {

        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(

    "ChoiceCar  Count: "+choiceCarCount);

        System.out.println(

    "ChoiceCar  Rate : " + df.format((float)choiceCarCount/playCount));

      }

    }

    //设置模式class Moder {

      String mode =

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

    int playTotalCount

    = 0; //玩的总次数

    //设置模式

    public void setMode() {

        Scanner sc =

    newScanner(System.in);

    //设置选门模式

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

    !mode.equals("B")) {

          System.out.println(

    "Plaseinput mode: A[Change]  B[Don'tChange]");

          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 =

    newScanner(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 =

    newThreeDoor();  //三门

        Player player =

    newPlayer();           //玩家

        Presenter presenter =

    newPresenter();  //主持人

        Scorer scorer =

    newScorer();           //计分者

        Moder moder =

    newModer();              //设置模式者

        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学习的分享就到这里了,大家是不是感觉很神奇呢,原来这个游戏套路这么深,希望大家能够学习到呢,记得收藏分享给身边的人哦。

    相关文章

      网友评论

          本文标题:小猿圈分享使用Java模拟三道门的游戏

          本文链接:https://www.haomeiwen.com/subject/rloitctx.html