2014-06-27 31 views
0
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其中一个非常相似,但似乎没有人真正帮助解决主要问题。蒙蒂大厅模拟没有按预期运行

我该怎么做才能让赔率更加真实?清除代码的建议也将受到赞赏。

编辑:代码现在功能,我现在只是想减少它。

+0

你可能会看到http://stackoverflow.com/questions/24208682/themontyhallparadox-is-this-right – pjs

回答

2

在你的方法withoutswitch您需要更改

if(guess==1) 
    nsCorrect++; 

if (curtains[guess] == 1) 
    nsCorrect++; 

相同的withswitch方法。在for循环的每次运行后,您需要将curtains重置为0.否则之前的1将位于该处,并且在几次运行后,curtains将仅包含1。

private static void resetCurtains(int[] curtains) { 
    for (int i = 0; i < curtains.length; i++) { 
    curtains[i] = 0; 
    } 
} 

for循环内每次运行后调用该方法。

此外,我会建议使用{}即使语句是1班轮:

if (curtrains[guess] == 1) { 
    nsCorrect++; 
} 
+0

这解决了这个问题,然后让我意识到,我需要设置每一次,所有的东西,所以我不会最终赢得的胜率非常高。谢谢! – Deusgiggity

0

我会备份和重新构建整个事情。游戏的一个目标是两个后代,其中一个只是挑选一扇门,其中一个挑选然后切换。

我并不完全遵循你的开关逻辑,但它绝对是错误的。蒙蒂的逻辑是,如果奖品在玩家的门后,你随机打开一个门,否则你打开一只山羊。只有一个随机数是可取的。

同样,玩家切换。此时只有一个窗帘,不需要随机数字。

在逻辑(不是Java,而不是整个程序)的粗糙刺:

MontyHaulGame 
{ 
    int[] Curtains = new int[3]; 
    int Car = Random(3); 
    int Guess; 
    Pick(); 
    if (Guess == Car) Wins++;  
} 

MontyHaulNoSwitch : MontyHaulGame 
{ 
    Pick() 
    { 
     Guess = Random(3); 
    } 
} 

MontyHaulSwitch : MontyHaulGame 
{ 
    Pick() 
    { 
     Guess = Random(3); 
     OpenOne(); 
     Switch(); 
    } 

    OpenOne() 
    { 
     if Guess == Car then 
       Repeat 
        Monty = Random(3); 
       Until Monty != Guess; 
     else 
      Monty = 1; 
      While (Monty == Guess) || (Monty == Car) 
        Monty++; 
    } 

    Switch() 
    { 
     NewGuess = 1; 
     While (NewGuess == Guess) || (NewGuess == Monty) 
      NewGuess++; 
     Guess == NewGuess; 
    } 
} 
2

如预期这两个你的方法不起作用。

你“withoutswitch”方法只选择0,1,或2个随机数,并增加了一个柜台时,其1换句话说,我可以简化您的“withoutswitch”的方法是:

public static void withoutswitch(int attempts, Random rand) { 
    int counter = 0; 
    for (int i = 0; i < attempts; i++) { 
     if (rand.nextInt(3) == 1) counter++; 
    } 
    System.out.println(counter); 
} 

你的“withswitch”方法,信不信由你,完全一样的东西。你开始使用一个变量进行猜测,然后你完全忽略它,并将其作为第二个变量,并检查它是否为1.因此,它会产生完全相同的结果。

您的两种方法都使用“窗帘”阵列,但不正确。这个问题的关键在于每次将汽车放在随机门后面,但是您从未将阵列重新设置为全零,因此,经过几次运行后,它会成为所有的阵列,这绝对不是什么你要。

下面是一些伪代码,以帮助您开始:

number of switch wins = 0 
number of stay wins = 0 
scan in the number of attempts 
loop through each attempt: 
    make an array {0, 0, 0} 
    pick a random number (0, 1, or 2), and set that array index to 1 (winning index) 
    pick a random number (0, 1, or 2), and set it to choice 
    pick a random number (0, 1, or 2), and set it to the shown door 
    loop while the door number isn't the choice number or the winning index: 
     pick a random number (0, 1, or 2) and set it to the shown door 
    increment stay wins if the choice is equal to the winning index 
    increment switch wins if (3 - choice - showndoor) is equal to the winning index 
print stay wins and switch wins 

注:逻辑的最后一点决定了开关门的指数,因为你只可能指数是0,1,2,( 0 + 1 + 2 = 3),然后3 - 你选择的门 - 你显示的门=最后一扇门。

希望这会有所帮助!

+0

我相信我修复了代码以使其发挥作用。你的第一部分花了我一些时间来解决。基本上,你已经把它简化为一个简单的“返回〜1/3的尝试”,对吧?我明白这就是我真正想要的,但我试图坚持蒙蒂霍尔的方式。如果有人正在阅读,他们是否能够正确识别它? – Deusgiggity

+0

你可能会误解我想说的话。我在说你写的方法本质上返回了1/3的尝试,就是这样,我建议你按照伪码纠正它们。 –