America used to have a variety show. Participants will see three closed doors, behind which there is a car, check the back of a car that can win the car door, the other two doors behind the possession of a goat each. When participants selected a door, but to open it, show host turned the remaining two doors of which one, which exposed a goat. Thereafter the participants will ask the host or want to change the door still shut on another fan. The question is: for the other contestants to win the car door will increase the chance of rate? The following is an analog three issues of Java code. import java.util.Scanner; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols;
// three class ThreeDoor {String [] threeDoor = new String [3]; // three
// public prize after setting the gate void setPrize () {int carPosition = (int) (Math.random () * 100)% 3;
threeDoor[0] = "Goat";
threeDoor[1] = "Goat";
threeDoor[2] = "Goat";
threeDoor[carPosition] = "Car";
复制代码
} }
// player class Player {int firstChoicePosition = 0; // select the first door int lastChoicePosition = 0; // final choice of door
A select gate // public void choiceDoor () {firstChoicePosition = (int) (Math.random () * 100)% 3; lastChoicePosition = firstChoicePosition;}
// replaced with another door public void changeDoor (int firstGoatPosition) {lastChoicePosition = 3 - firstChoicePosition - firstGoatPosition;}}
// host class Presenter {int firstGoatPosition = 0; // host openable door
//打开后面为山羊的门 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; } } }
// scoreboard class Scorer {int playCount = 0; // get the number of goats; the total number // play int choiceCarCount = 0; // get the number of cars 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)); } }
// Set mode class Moder {String mode = ""; // mode, A: Door change, B: no change door int playTotalCount = 0; // total number of play
// Set Mode 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();
复制代码
}
// set the display mode of 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 program class ThreeDoorDemo {public static void main (String [] args) {ThreeDoor threeDoor = new ThreeDoor (); // three Player player = new Player (); // player Presenter presenter = new Presenter (); / / moderator Scorer scorer = new Scorer (); // scorer Moder moder = new Moder (); // setting mode by
moder.setMode(); //设置模式
moder.showMode(); //显示模式
//循环玩多次
for(int i=0; i<moder.playTotalCount; 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(); //统计获得汽车的概率
复制代码
} }
Well, today a small circle apes share learning about Java on here, we are not very magical feeling it, the original routine so deep in this game, I hope you can learn to do, remember to share collection of people around, oh.
Reproduced in: https: //juejin.im/post/5cf080bb6fb9a07edd29f7d6