Monty hall模拟未按预期运行 import java.util.Scanner; 导入静态java.lang.System.*; 导入静态java.lang.Math.*; 导入java.util.Random; 公务舱月池 { 公共静态void main(字符串[]args) { 扫描仪键盘=新扫描仪(System.in); System.out.print(“输入尝试次数:”); int尝试次数=keyboard.nextInt() 跑。数字越大,你得到的答案就越现实*/ int窗帘[]=new int[3];//就像游戏节目一样,其中一个会是赢家 Random rand=new Random();/*使用相同的随机种子设置方法,该种子 否则,这些变化将以毫秒为单位,并可能影响响应*/ 无开关(窗帘、尝试、兰德); 带开关(窗帘、尝试、兰德); } 不带开关的公共静态无效(int窗帘[],int尝试,随机rand) { int nsCorrect=0; for(int x=0;x

Monty hall模拟未按预期运行 import java.util.Scanner; 导入静态java.lang.System.*; 导入静态java.lang.Math.*; 导入java.util.Random; 公务舱月池 { 公共静态void main(字符串[]args) { 扫描仪键盘=新扫描仪(System.in); System.out.print(“输入尝试次数:”); int尝试次数=keyboard.nextInt() 跑。数字越大,你得到的答案就越现实*/ int窗帘[]=new int[3];//就像游戏节目一样,其中一个会是赢家 Random rand=new Random();/*使用相同的随机种子设置方法,该种子 否则,这些变化将以毫秒为单位,并可能影响响应*/ 无开关(窗帘、尝试、兰德); 带开关(窗帘、尝试、兰德); } 不带开关的公共静态无效(int窗帘[],int尝试,随机rand) { int nsCorrect=0; for(int x=0;x,java,algorithm,Java,Algorithm,很抱歉,这有点混乱,在中断了将近一年之后,我正试图重新开始编写代码。第一部分按预期运行,大约有1/3的成功机会。然而,第二个,应该给我2/3的机会,但我仍然得到了与没有开关大致相同的数量。我浏览了整个网站,发现了很多我不熟悉的Java以外的东西。其中一个非常相似,但似乎没有人真正帮助解决主要问题 我能做些什么使赔率更现实?关于清理代码的建议也将不胜感激 编辑:CodeNow功能正常,我现在只是想缩小它的尺寸。在您的方法中不带开关您需要更改 import java.util.Scanner

很抱歉,这有点混乱,在中断了将近一年之后,我正试图重新开始编写代码。第一部分按预期运行,大约有1/3的成功机会。然而,第二个,应该给我2/3的机会,但我仍然得到了与没有开关大致相同的数量。我浏览了整个网站,发现了很多我不熟悉的Java以外的东西。其中一个非常相似,但似乎没有人真正帮助解决主要问题

我能做些什么使赔率更现实?关于清理代码的建议也将不胜感激


编辑:CodeNow功能正常,我现在只是想缩小它的尺寸。

在您的方法中不带开关您需要更改

    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);
        }   

}

与使用开关的
方法相同。每次运行for循环后,需要将
窗帘
重置为0。否则,前面的
1
将在那里,运行几次后
窗帘将只包含1

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

在不带开关的方法
中,您需要更改

    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);
        }   

}

与使用开关的
方法相同。每次运行for循环后,需要将
窗帘
重置为0。否则,前面的
1
将在那里,运行几次后
窗帘将只包含1

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

我会支持和重组整个事情。游戏的一个对象,两个后代,其中一个简单地选择一扇门,其中一个选择然后切换

我并没有完全遵循你的切换逻辑,但这绝对是错误的。蒙蒂的逻辑是,如果奖品在玩家的门后,你随机打开一个,否则你会用山羊打开一个。只需要一个随机数

同样,播放器也会切换。此时只有一个窗帘,不需要随机数字

粗略地分析一下逻辑(不是Java,不是整个程序):


我会支持和重组整个事情。游戏的一个对象,两个后代,其中一个简单地选择一扇门,其中一个选择然后切换

我并没有完全遵循你的切换逻辑,但这绝对是错误的。蒙蒂的逻辑是,如果奖品在玩家的门后,你随机打开一个,否则你会用山羊打开一个。只需要一个随机数

同样,播放器也会切换。此时只有一个窗帘,不需要随机数字

粗略地分析一下逻辑(不是Java,不是整个程序):


您的两种方法都无法按预期运行

您的“withoutswitch”方法仅从0、1或2中选择一个随机数,并在其为1时添加到计数器。换句话说,我可以将您的“无开关”方法简化为:

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;
    }
}
注意:逻辑的最后一位决定了切换门的索引,因为您唯一可能的索引是0、1和2(0+1+2=3),那么3-您选择的门-显示的门=最后一个门


希望这有帮助

这两种方法的功能都不符合预期

您的“withoutswitch”方法仅从0、1或2中选择一个随机数,并在其为1时添加到计数器。换句话说,我可以将您的“无开关”方法简化为:

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;
    }
}
注意:逻辑的最后一位决定了开关门的索引,因为您唯一可能的索引
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);
}
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