java - Monty Hall 让我们做一笔交易 用 java 模拟(10000 次迭代)

标签 java if-statement switch-statement logic probability

我要创建一个程序,运行 Monty Hall 的交易 10000 次并输出以下统计数据:

  • 胜负次数
  • 玩家切换并获胜与留下的次数 获胜。
  • 显示切换和停留时的胜负百分比。

切换时,切换并获胜与停留并获胜的期望输出应该在 2/3 左右。我得到了,但只得到了一半;更换时为 33%,留下时为 16%。我不知道为什么另外 50% 没有出现。 很确定它与我的第二个 switch 语句有关,但无法弄清楚(可能是由于 sleep 不足)。

我做错了什么?提前致谢。

package pic;

import java.util.Scanner;

public class PartAB {
    public static void main(String[] args) {
        System.out.println("WELCOME TO 'LETS MAKE A DEAL'");
        System.out
                .println("Please Enter 'A' to Play, 'B' To Watch, or 'Q' To Quit");
        Scanner input = new Scanner(System.in);
        String choice = input.next();

        boolean done = false;
        double wins = 0;
        double games = 0;
        double switches = 0;
        double noSwitch = 0;
        int iterations;
        for (iterations = 0; iterations < 10000; iterations++) {
            int prizeIs = (int) ((Math.random() * 3) + 1);
            int compChoice = (int) ((Math.random() * 3) + 1);
            int zonkIs = 0;
            if (prizeIs == compChoice) {
                boolean chooseFirstZonk = Math.random() < 0.5; // 50% chance
                switch (prizeIs) {
                case 1:
                    if (chooseFirstZonk) {
                        zonkIs = 2;
                    } else {
                        zonkIs = 3;
                    }
                    break;
                case 2:
                    if (chooseFirstZonk) {
                        zonkIs = 1;
                    } else {
                        zonkIs = 3;
                    }
                    break;
                case 3:
                    if (chooseFirstZonk) {
                        zonkIs = 1;
                    } else {
                        zonkIs = 2;
                    }
                    break;
                }
            } else if (prizeIs == 1 && compChoice == 2) {
                zonkIs = 3;
            } else if (prizeIs == 1 && compChoice == 3) {
                zonkIs = 2;
            } else if (prizeIs == 2 && compChoice == 1) {
                zonkIs = 3;
            } else if (prizeIs == 2 && compChoice == 3) {
                zonkIs = 1;
            } else if (prizeIs == 3 && compChoice == 1) {
                zonkIs = 2;
            } else if (prizeIs == 3 && compChoice == 2) {
                zonkIs = 1;
            }

            // generating a 1 or 2 to decide whether to switch doors or not
            int switchDoor = (int) (1 + (Math.random() * 2));

            switch (switchDoor) {
            // not switching doors
            case 1: {
                // since we didnt switch
                // if compchoice == prize
                // then thats considered a win
                // for not switching
                if (compChoice == prizeIs) {
                    noSwitch++;
                    wins++;
                    games++;
                }
                // if we didnt win
                // the games will be incremented by 1
                // later to use to calculate the losses
                else {
                    games++;
                }
            }
                break;
            // switch door
            case 2: {
                // since we did switch
                // if compchoice == prize
                // then thats considered a loss
                // because were switching
                // to the door that has a zonk
                if (compChoice == prizeIs) {
                    games++;
                }

                // if compchoice != prize
                // then thats considered a win
                // because were switching from the door
                // with a zonk to the door with the prize
                else if (compChoice != prizeIs) {

                    switches++;
                    wins++;
                    games++;
                }

            }
            }
            if (iterations == 10000) {

                double percentage = 100.0 * (switches / games);
                double noswitchpercentage = 100.0 * (noSwitch / games);
                System.out.println("Switch percentage : " + percentage);
                System.out.println("No Switch percentage : "
                        + noswitchpercentage);
                System.out.println("wins : " + wins);
                System.out.println("losses : " + (games - wins));
                break;
            }
        }
    }
}

最佳答案

您犯了一些逻辑错误,在您更正的switch (switchDoor)代码段中可能会这样

switch (switchDoor) {

case 1: {

    if (compChoice == prizeIs) {
        noSwitch++;
        wins++;
        games++;
    }
    else {
        games++;
        switches++;  //This should be added, game incremented but switch or noswitch can not. 50% time may this happens.
                    // If we chose no Switch and loose that means switches should be right choice and get incremented.

    }
}
    break;

case 2: {
    if (compChoice == prizeIs) {
        games++;
        noSwitch++; //Same as previous logic
    }
    else if (compChoice != prizeIs) {

        switches++;
        wins++;
        games++;
    }

}

关于java - Monty Hall 让我们做一笔交易 用 java 模拟(10000 次迭代),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33023854/

相关文章:

javascript - Javascript 中 boolean 语句是如何计算的

c - while、switch、case语句

javascript - 根据 ESLint,Switch 语句默认返回 "Unreachable"

java - 为什么 JBoss 总是重新部署已经部署但未更改的 .WAR 文件?

java - 在 Oracle 12c 上插入/检索阿拉伯语数据

java - 为什么我无法使用 Spring Boot 在 Spring 上下文中获取可用的 Bean 定义?

javascript - JavaScript中多个case的switch语句

java - 多幻灯片表示

java - 为什么 Java 看不到整数相等?

php - (PHP) IF-ELSE 同时两个条件