Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hey I am making a Monty Hall code and it seems like my percentages are being cut in half or something. What needs to be

Hey I am making a Monty Hall code and it seems like my percentages are being cut in half or something. What needs to be corrected?

import java.lang.Math; // headers MUST be above the first class import java.text.DecimalFormat; public class CarsAndGoats { public static void main(String[] args) { //play 1000 games and gather results without switching doors boolean[] noChange = new boolean[1000]; boolean[] withChange = new boolean[1000]; //create and populate the prize doors boolean[] doors = new boolean[4]; for (int times = 0; times < noChange.length; times++) { // setting the doors prizes placePrizes(doors); noChange[times] = gameRunner(doors, false); } for (int times = 0; times < withChange.length; times++) { placePrizes(doors); withChange[times] = (boolean) gameRunner(doors, true); withChange[times] = (boolean) gameRunner(doors, true); } System.out.println("Probability of Winning with Change: " + countPrizes(noChange) + "%"); ; System.out.println("Probability of Winning without Change:" + countPrizes(withChange) + "%"); } public static boolean gameRunner(boolean[] doors, boolean changeDoor) //Runs game { int playerChoice = (int) (Math.random() * 3) +1; System.out.println("Players first choice: " + playerChoice); for (int x = 0; x < doors.length; x++) { if (!doors[x] && x != playerChoice) { break; } } if (changeDoor) { int randomNumber = +1; int originalChoice = playerChoice; do { randomNumber = (int) (Math.random() * 3); } while (!(randomNumber != originalChoice && randomNumber != playerChoice)); playerChoice = randomNumber; System.out.println("The players second choice is: " + playerChoice); } return doesPlayerWin(playerChoice, doors); } public static void placePrizes(boolean[] doors) { for (int times = 0; times < doors.length; times++) { doors[times] = false; } int carDoor = (int) (Math.random() * 3) +1; System.out.println("The Car is Hidden in Door: " + carDoor); doors[carDoor] = true; } // end of placePrizes method { static boolean doesPlayerWin(int playerChoice, boolean[] doors) { { if (doors[playerChoice]) { System.out.println("You won!"); return true; } else { System.out.println("You lost..."); return false; } } } public static double countPrizes(boolean[] wins) { DecimalFormat df = new DecimalFormat("#.00"); df.format(0.0); int winsCount = 0; new DecimalFormat("#.##").format(winsCount); new DecimalFormat("#.##").format(wins.length); for (int x = 0; x < wins.length; x++) { if (wins[x]) { winsCount++; } } return ((double) winsCount / (double) wins.length) * 100.0; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Fundamentals Study Guide

Authors: Dr. Sergio Pisano

1st Edition

B09K1WW84J, 979-8985115307

More Books

Students also viewed these Databases questions

Question

Draw a labelled diagram of the Dicot stem.

Answered: 1 week ago

Question

LO3 Describe the two most common methods of applying for a job.

Answered: 1 week ago

Question

LO1 Explain the strategic importance of the recruitment function.

Answered: 1 week ago