Java 蒙蒂霍尔游戏

Java 蒙蒂霍尔游戏,java,logic,probability,Java,Logic,Probability,我是一名新程序员,目前正在学习Java。我有一个基于游戏的游戏,让我们做个交易&蒙蒂霍尔问题(),我一直对代码的逻辑有问题。除了我无法让用户门切换到另一扇门并确定他们是否是正确的赢家之外,一切似乎都正常工作。如果有人能帮助我理解我在这里做错了什么,我愿意借此机会学习,谢谢 import java.util.Random; import java.util.Scanner; public class GameShow { public static void main(String[] args

我是一名新程序员,目前正在学习Java。我有一个基于游戏的游戏,让我们做个交易&蒙蒂霍尔问题(),我一直对代码的逻辑有问题。除了我无法让用户门切换到另一扇门并确定他们是否是正确的赢家之外,一切似乎都正常工作。如果有人能帮助我理解我在这里做错了什么,我愿意借此机会学习,谢谢

import java.util.Random;
import java.util.Scanner;
public class GameShow {

public static void main(String[] args) {
    Scanner scan = new Scanner (System.in);
    Random generator = new Random();

    // Initialize Variables
    int user_door,
        open_door,
        other_door,
        prize_door;

    // Generate random value 1-3
    prize_door = generator.nextInt(3)+1;
    open_door = prize_door;

    while(open_door == prize_door){
        open_door = generator.nextInt(3)+1;
    }

    other_door = open_door;

    while (other_door == open_door || other_door == prize_door){
        other_door = generator.nextInt(3)+1;
    }

    // Begin Game
    System.out.println("*** Welcome to the game show! ***");  
    System.out.println("Select the door (1, 2, or 3): ");
    user_door = scan.nextInt();


    // User Validation
        if (user_door > 3 || user_door < 0) {
            System.out.println("Please select door 1, 2, or 3");
            user_door = scan.nextInt();
        } else if(user_door == 1 || user_door == 2 ||  user_door == 3) {

    //Continue Game
    System.out.println("\nIn a moment, I will show you where the prize is located,");
    System.out.println("but first I will show you what is behind one of the other doors");

    //Continue Dialogue
    System.out.println("\nBehind door number " + open_door + " are goats!");
    System.out.println("You selected door number " + user_door);
    System.out.println("\nWould you like to switch your door(y/n)? ");

    //User Input Yes or No
    char userReply = scan.next().charAt(0);

    //If statement with nested while statements for user input
        if (userReply == 'y'){
                user_door = other_door;
                } while(userReply != 'y' && userReply != 'n')
                {
                    //User Validation
                    System.out.println("Please enter either y/n");
                    userReply = scan.next().charAt(0);
                } 

    System.out.println("The prize is behind door number: " + prize_door); 

    //Check to see if user won or lost
        if(user_door == prize_door){
            System.out.println("Congratulations! You won the prize!");
            } else {
                    System.out.println("Sorry. You lost.");
                    }       

        }
    }
}
import java.util.Random;
导入java.util.Scanner;
公开课游戏表演{
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
随机生成器=新随机();
//初始化变量
int user_门,
打开门,
另一扇门,
奖品门;
//生成随机值1-3
奖品门=发电机。下一个(3)+1;
开门=奖品门;
while(开门==奖品门){
开门=发电机。下一个(3)+1;
}
其他门=打开门;
while(其他门==打开门| |其他门==奖品门){
其他门=发电机。下一个(3)+1;
}
//开始游戏
System.out.println(“***欢迎来到游戏秀!***”);
System.out.println(“选择门(1、2或3):”;
user_door=scan.nextInt();
//用户验证
如果(用户门>3 | |用户门<0){
System.out.println(“请选择门1、门2或门3”);
user_door=scan.nextInt();
}else if(用户门==1 | |用户门==2 | |用户门==3){
//继续比赛
System.out.println(“\n稍后,我将告诉您奖品的位置,”);
System.out.println(“但首先我将向您展示另一扇门后面是什么”);
//继续对话
System.out.println(“\n后门编号“+开门+”是山羊!”);
System.out.println(“您选择的门号”+用户门);
System.out.println(“\n是否要切换门(是/否)”;
//用户输入是或否
char userReply=scan.next().charAt(0);
//用于用户输入的带有嵌套while语句的If语句
如果(userReply='y'){
用户门=其他门;
}while(userReply!=“y”&&userReply!=“n”)
{
//用户验证
System.out.println(“请输入是/否”);
userReply=scan.next().charAt(0);
} 
System.out.println(“奖品在门号后面:“+奖品门”);
//检查用户是赢了还是输了
如果(用户门==奖品门){
System.out.println(“恭喜!你获奖了!”);
}否则{
System.out.println(“对不起,你输了。”);
}       
}
}
}

让我们通过一个示例一步一步地了解代码的功能

import java.util.Random;
import java.util.Scanner;
public class GameShow {

public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
Random generator = new Random();

// Initialize Variables
int user_door,
    open_door,
    other_door,
    prize_door;

// Generate random value 1-3
prize_door = generator.nextInt(3)+1; 
假设生成器将2分配给
price\u door

open_door = prize_door;

while(open_door == prize_door){
    open_door = generator.nextInt(3)+1;
}
other_door = open_door;

while (other_door == open_door || other_door == prize_door){
    other_door = generator.nextInt(3)+1;
}
// Begin Game
System.out.println("*** Welcome to the game show! ***");  
System.out.println("Select the door (1, 2, or 3): ");
假设在循环之后,生成器将1分配给
open\u door

open_door = prize_door;

while(open_door == prize_door){
    open_door = generator.nextInt(3)+1;
}
other_door = open_door;

while (other_door == open_door || other_door == prize_door){
    other_door = generator.nextInt(3)+1;
}
// Begin Game
System.out.println("*** Welcome to the game show! ***");  
System.out.println("Select the door (1, 2, or 3): ");
让我们假设在循环之后,生成器将3分配给
other\u door

open_door = prize_door;

while(open_door == prize_door){
    open_door = generator.nextInt(3)+1;
}
other_door = open_door;

while (other_door == open_door || other_door == prize_door){
    other_door = generator.nextInt(3)+1;
}
// Begin Game
System.out.println("*** Welcome to the game show! ***");  
System.out.println("Select the door (1, 2, or 3): ");
让我们回忆变量的值
open\u door=1
other\u door=3
prize\u door=2

user_door = scan.nextInt();
// User Validation
    if (user_door > 3 || user_door < 0) {
        System.out.println("Please select door 1, 2, or 3");
        user_door = scan.nextInt();
    } else if(user_door == 1 || user_door == 2 ||  user_door == 3) {
这里您正在打开门1(
open\u Door=1
),它与
user\u Door=1

根据游戏逻辑,哪个是错误的

System.out.println("You selected door number " + user_door);
System.out.println("\nWould you like to switch your door(y/n)? ");

//User Input Yes or No
char userReply = scan.next().charAt(0);

//If statement with nested while statements for user input
    if (userReply == 'y'){
            user_door = other_door;
            } while(userReply != 'y' && userReply != 'n')
            {
                //User Validation
                System.out.println("Please enter either y/n");
                userReply = scan.next().charAt(0);
            } 

System.out.println("The prize is behind door number: " + prize_door); 

//Check to see if user won or lost
    if(user_door == prize_door){
        System.out.println("Congratulations! You won the prize!");
        } else {
                System.out.println("Sorry. You lost.");
                }       

    }
}
}
您需要根据用户的输入更改门的选择,因为作为主机,您知道汽车/黄金在哪里,以及程序中缺少的山羊在哪里


您应该在游戏开始前修复
奖品门
,但
打开门
其他门
应根据
用户门

确定,因为您从未正确分配其他门。您分配的另一扇门是一个随机数,而且您的实现没有正确描述Monty Hall问题

open_door = prize_door;

while(open_door == prize_door){
    open_door = generator.nextInt(3)+1;
}
other_door = open_door;

while (other_door == open_door || other_door == prize_door){
    other_door = generator.nextInt(3)+1;
}
// Begin Game
System.out.println("*** Welcome to the game show! ***");  
System.out.println("Select the door (1, 2, or 3): ");
中断程序的示例 所以我们假设奖品门=1,打开门=2,然后默认情况下另一扇门=3

假设用户选择了门2。然后,当门2打开时,实际上有两个可能的其他门:1和3

Monty Hall问题的一个关键部分是用户未选择的门被打开

选择用户的门时,需要确定另一扇门是什么。这段代码有点难以理解,因此我将回顾在试图查找您的问题时发现的一些问题

奖门一代 这段代码正确地分配了奖品门,但其余的不需要完成。我们可以将它的实现简化到这一点

prize_door = generator.nextInt(3) + 1; //we know what the prize is
这比3个变量更容易管理,并且对随机性的依赖性更小。我们只需要跟踪选择者的门,以及奖品在后面的门

获取用户的门 在最初的Monty Hall游戏的这一点上,你应该打开一扇选择器没有选择的门。用户的门从未打开过。在这种情况下,我们有两个输入,用户门和奖品门 使用俗套的逻辑,我们可以通过将它们加在一起来确定打开哪扇门,或者你可以做一个“如果”验证。我将介绍cheesy,因为它写起来更快,而且仍然有意义

if(prize_door + user_door == 4) { // 1 and 3
    open_door = 2;
}
else if(prize_door + user_door == 5) { // 2 and 3
    open_door = 1;
}
else { //1 and 2
    open_door = 3;
}
不起眼的选择是:

boolean oneIsTaken = false;
boolean twoIsTaken = false;

if(prize_door == 1 || user_door == 1) {
    oneIsTaken = true;
}
if(prize_door == 2 || user_door == 2) {
    twoIsTaken = true;
}

if(oneIsTaken && twoIsTaken) {
    open_door = 3;
}
else if(oneIsTaken) {
    open_door = 2;
}
else {
    open_door = 1;
}
找到你可以换到的门 使用打开的门和用户的原始门,我们可以选择切换门,因为这两个门不是您将切换到的门

if(open_door + user_door == 4) { // 1 and 3
    user_door = 2;
}
else if(open_door + user_door == 5) { // 2 and 3
    user_door = 1;
}
else { //1 and 2
    user_door = 3;
}
确定用户是否已获胜 同上:

    if(user_door == prize_door){
        System.out.println("Congratulations! You won the prize!");
        } else {
                System.out.println("Sorry. You lost.");
                }       

    }

这没有问题。

我同意斯里坎特上面所说的。缺少的是测试用户回复的部分。您应该切换“if”和“while”语句。这将有助于解决您的车门开关问题

以前

 if (userReply == 'y')
     {
     user_door = other_door;
     }  
 while(userReply != 'y' && userReply != 'n')
     {
     //User Validation
     System.out.println("Please enter either y/n");
     userReply = scan.next().charAt(0);
     } 
改为

 while(userReply != 'y' && userReply != 'n')
    {
        //User Validation
        System.out.println("Please enter either y/n");
        userReply = scan.next().charAt(0);
    }
 if (userReply == 'y')
    {
     user_door = other_door;
    } 
您的操作方式仅在第一次输入字符时进行检查。如果他们做错了,并试图再做一次,那么它会