MontyHall java代码。帮助java初学者

时间:2016-01-31 23:12:52

标签: java

我是一个相当新的java,作为我的第一个任务,我需要使用两个类来模拟MontyHall游戏。门和MontyHall。

我的主要问题是MontyHall类。我无法模拟随机门的选择并在此之后继续进行。任何帮助或暗示让我朝着正确的方向前进都会有很大的帮助。

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

   }

1 个答案:

答案 0 :(得分:-2)

此难度的主要原因是您在程序中使用的随机功能。事实上,它不是有效的,它是java(和其他编程语言)中的经典问题。要了解有关此主题的更多信息,请查看此post

这个post可以很好地解决您的问题。

希望这有助于。