MontyHall java代码。java初学者帮助
我对java相当陌生,作为我的第一个任务,我需要使用两个类来模拟MontyHall游戏。门和门 我的主要问题是关于班上的MontyHall。我很难模拟随机门的选择,然后再继续。任何帮助或提示,使我在正确的方向将是很大的帮助MontyHall java代码。java初学者帮助,java,Java,我对java相当陌生,作为我的第一个任务,我需要使用两个类来模拟MontyHall游戏。门和门 我的主要问题是关于班上的MontyHall。我很难模拟随机门的选择,然后再继续。任何帮助或提示,使我在正确的方向将是很大的帮助 import java.util.Random; public class MontyHall { // ADD YOUR INSTANCE VARIABLES HERE private int door1; //door that holds the pr
import java.util.Random;
public class MontyHall {
// ADD YOUR INSTANCE VARIABLES HERE
private int door1; //door that holds the prize
private int door2; //one of the remaining two doors that doesnt have a prize
private int door3; //the last remaining door that doesnt have a prize
private int door4; //a door that the user chooses
Random generator = new Random();
/**
* Initializes the three doors.
*/
public MontyHall(){
// REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATION
Door door1 = new Door("prizeDoor");
Door door2 = new Door("hostDoor");
Door door3 = new Door("finalDoor");
Door door4 = new Door("selectedDoor");
}
/**
* Simulates one Monty Hall game.
* <ol>
* <li>Resets all three doors</li>
* <li>Simulates the selection of one of the doors by the player</li>
* <li>Simulates opening of an empty door by the host</li>
* <li>prints the outcome for switching and not switching door to standard output</li>
* </ol>
*/
public void oneGame(){
// REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATION
int door1 = generator.nextInt(3)+1; //assign a value for door1
System.out.println("The prize was behind door " + door1);
while(door2 == door1){
int door2 = generator.nextInt(3)+1; //assign a value for door2
}
while(door3 == door1 || door3 == door2){
int door3 = generator.nextInt(3)+1; //assign a value for door3
}
if (((door4 == door1) && (Door.SelectedByPlayer() == true)) || ((door4 == door2) && (Door.SelectedByPlayer() == true))){
System.out.println("Switching strategy would have lost");
} else{
System.out.println("Switching strategy would have won");
}
}
/**
* Simulates a random selection of one of the three doors.
* @return the door randomly selected
*/
private int pickADoor(){
// REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATION
int door4 = generator.nextInt(3)+1;
return this.door4;
System.out.println("The player selected door " + door4);
}
/**
* Simulates the opening of one of the other doors once the player selected one.
* It should open a door chosen randomly among the ones that don't have the prize and
* that are not selected by the player
*
* @param prizeDoor the door that hides the prize
* @param selectedDoor the door that was selected by the player
* @return the door opened
*/
private int openOtherDoor(int prizeDoor, int selectedDoor){
// REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATION
prizeDoor = door1;
selectedDoor = door4;
if(selectedDoor == prizeDoor){
int hostDoor = generator.nextInt(3)+1;
}
while(door2 != selectedDoor && door2 != prizeDoor){
int door2 = generator.nextInt(3)+1;
}
return this.door2;
System.out.println("The host opened door " + door2);
}
import java.util.Random;
公务舱月池{
//在此处添加实例变量
私人int door1;//持有奖品的门
private int Door 2;//剩余两个没有奖品的门之一
private int door 3;//剩下的最后一扇没有奖品的门
private int door4;//用户选择的门
随机生成器=新随机();
/**
*初始化三个门。
*/
公共月池(){
//用您自己的实现替换此方法的主体
门1=新门(“prizeDoor”);
门2=新门(“主机门”);
门3=新门(“最终门”);
门4=新门(“选定门”);
}
/**
*模拟一个Monty Hall游戏。
*
*重置所有三扇门
*模拟玩家对其中一扇门的选择
*模拟主机打开空门
*将开关和不开关门的结果打印到标准输出
*
*/
公共游戏{
//用您自己的实现替换此方法的主体
int door1=generator.nextInt(3)+1;//为door1赋值
System.out.println(“奖品在门后”+door1);
while(door2==door1){
int door2=generator.nextInt(3)+1;//为door2赋值
}
while(door3==door1 | | door3==door2){
int door3=generator.nextInt(3)+1;//为door3赋值
}
如果(((door4==door1)和&(Door.SelectedByPlayer()==true))| |((door4==door2)和&(Door.SelectedByPlayer()==true))){
System.out.println(“切换策略会丢失”);
}否则{
System.out.println(“切换策略会赢”);
}
}
/**
*模拟随机选择三扇门中的一扇门。
*@返回随机选择的门
*/
私有int pickador(){
//用您自己的实现替换此方法的主体
int door4=发电机下一个(3)+1;
把这个还给我;
System.out.println(“玩家选择的门”+door4);
}
/**
*当玩家选择一扇门时,模拟另一扇门的打开。
*它应该打开一扇门,从没有奖品的人中随机选择,然后
*不是玩家选择的
*
*@param prizeDoor隐藏奖品的门
*@param selectedDoor玩家选择的门
*@门开了就回来
*/
私人int打开其他门(int prizeDoor、int selectedDoor){
//用您自己的实现替换此方法的主体
prizeDoor=门1;
选择门=门4;
if(selectedDoor==prizeDoor){
int hostDoor=generator.nextInt(3)+1;
}
while(门2!=已选门和门2!=prizeDoor){
int door2=发电机下一个(3)+1;
}
把这个还给我;
System.out.println(“主机开门”+door2);
}
造成此困难的主要原因是您在程序中使用的随机函数。事实上,它效率不高,是java(和其他编程语言)中的一个经典问题。要了解有关此主题的更多信息,请查看此主题
这对您的问题是一个很好的指导
希望这有帮助。一个词-。欢迎来到StackOverflow。请花一些时间访问并阅读。您的代码中似乎存在一些基本的概念性问题,包括Java和一般编程方面的问题,这些问题有些超出了StackOverflow的预期范围。因此,这里不是讨论的地方,也不是您可以获得教程的地方ls或涵盖其他地方可获得的基本信息。对于您的情况,我认为与教授或助教交谈是正确的方向。@samer alsaadi:您已经了解数组了吗?另外……考虑门的状态和操作。例如:有些门后面有山羊-可能是您可以将其表示为布尔值?此外,门可以是.open()ed.hmmm…因此,它们可能需要跟踪是否已打开。此外,MountyHall选择(ADOR),也许?:)@luizTavares你把我引向了正确的方向。谢谢。为了跟踪门是否打开,if语句是否有用?你的措辞是误导性的。这个错误在OP的代码中,这与“java(和其他编程语言)”完全无关;它与基本逻辑有关。