Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You will prompt the user to enter the number of times he or she wants to play th

ID: 3640271 • Letter: Y

Question

You will prompt the user to enter the number of times he or she wants to play this game.
You will create a variable to keep track of the number of times the contestant wins by switching.
Generate a random number between 1 and 3 (inclusive) that will denote the door that conceals the prize.
Generate another random number between 1 and 3 (inclusive) that will denote the guess the contestant makes.
From those two numbers, compute a number that does not conceal the prize nor is it the contestant's guess. This is the door that is opened by Monty Hall and we shall call it view.
At this point the contestant changes his mind makes a newGuess that is not the original guess nor is it the view.
You will compare the value of the newGuess with that of prize. If they are the same, the contestant won by switching, and you increment the variable that was keeping track of that.
You will run this simulation for however many times the user had specified.
To obtain the probability for winning if you switch divide the number of times the contestant wins by switching by the total number of games played.
To obtain the probability of winning by not switching subtract the above number from one.
A sample session and output will look as follows:

Enter number of times you want to play: 10

Prize Guess View New Guess
2 2 3 1
1 3 2 1
1 1 2 3
3 1 2 3
3 1 2 3
3 1 2 3
2 2 3 1
2 1 3 2
3 2 1 3
2 1 3 2

Probability of winning if you switch = 0.70
Probability of winning if you do not switch = 0.30

Explanation / Answer

package deal;

import java.util.*;

public class Deal {
   private static Scanner scanner = new Scanner(System.in);
   private static Random generator = new Random();
  
   public static void main(String[] args) {
       int times = 0;
       int count = 0;
       int prize = 0;
       int guess = 0;
       int view = 0;
       int newGuess = 0;
       int winsBySwitch = 0;
      
       System.out.print("Enter number of times you want to play: ");
       times = scanner.nextInt();
       count = times;
       System.out.println(" Prize   Guess   View   New Guess");
      
       while (count != 0) {
           prize = generator.nextInt(3) + 1;
           guess = generator.nextInt(3) + 1;
           view = generator.nextInt(3) + 1;
           while (view == guess || view == prize) {
               view = generator.nextInt(3) + 1;
           }
           for (int i = 1; i <= 3; i++) {
               if (i != guess && i != view) {
                   newGuess = i;
               }
           }
           if (newGuess == prize) {
               winsBySwitch++;
           }
           System.out.println(" " + prize + "       " + guess + "       " + view + "        " + newGuess);
           count--;
       }
      
       System.out.printf(" Probability of winning if you switch = %.2f ", (double) winsBySwitch / times);
       System.out.printf("Probability of winning if you do not switch = %.2f ", 1.0 - (double) winsBySwitch / times);
      
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote