2014-06-27 38 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的嘗試,就是這樣,我建議你按照僞碼糾正它們。 –