import java.util.Scanner;
import static java.lang.System.*;
import static java.lang.Math.*;
import java.util.Random;
public class Montyhall
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter number of attempts:");
int attempts = keyboard.nextInt();/*Decide how many times you want this to
run. The larger the number, the more realistic an answer you will get.*/
int curtains[] = new int[3]; //like the game show, one of these will be a winner
Random rand = new Random(); /*sets the methods up with the same random seed, which
otherwise changes by the milisecond and could influence the response*/
withoutswitch(curtains, attempts, rand);
withswitch(curtains, attempts, rand);
}
public static void withoutswitch(int curtains[], int attempts, Random rand)
{
int nsCorrect=0;
for(int x=0; x < attempts; x++)
{
int winner = rand.nextInt(3);//Sets the winner to 1, leaving the other two at 0.
curtains[winner] = 1; //It then checks to see if they are the same,
int guess = rand.nextInt(3); //a 1/3 chance, roughly.
if(curtains[guess]==1){
nsCorrect++;
}
curtains[0]=0;
curtains[1]=0;
curtains[2]=0;
//the player never changes their decision, so it does not matter if a door is opened.
}
System.out.println("Number of successes with no switch: " + nsCorrect);
}
public static void withswitch(int curtains[], int attempts, Random rand)
{
int ysCorrect=0;
int goat = 0;
for(int x=0; x < attempts; x++)
{
int winner = rand.nextInt(3);
curtains[winner] = 1;
int guess = rand.nextInt(3);
goat = rand.nextInt(3);//one of the doors is opened
while(goat == winner || goat == guess)//the opened door is randomized until
goat = rand.nextInt(3); //it isn't the guess or the winner.
int guess2 = rand.nextInt(3);
while(guess2 == guess || guess2 == goat)
guess2 = rand.nextInt(3);//the second guess goes through a similar process
if(curtains[guess2]==1){
ysCorrect++;
}
curtains[0]=0;
curtains[1]=0;
curtains[2]=0;
}
System.out.println("Number of successes with a switch: " + ysCorrect);
}
}
对不起,这有点凌乱,我试图回到编码后几乎一年的间隙。第一部分的功能如预期,大概有1/3的成功机会。然而,第二个应该给我一个2/3的机会,但是我仍然可以获得与开关大致相同的数量,就像没有开关一样。我浏览了这个网站,大部分发现了我不熟悉的Java之外的东西。 This其中一个非常相似,但似乎没有人真正帮助解决主要问题。蒙蒂大厅模拟没有按预期运行
我该怎么做才能让赔率更加真实?清除代码的建议也将受到赞赏。
编辑:代码现在功能,我现在只是想减少它。
你可能会看到http://stackoverflow.com/questions/24208682/themontyhallparadox-is-this-right – pjs