samedi 30 janvier 2021

How to make Monty Hall game in Java? [closed]

I'm trying to create the Monty Hall game in Java.

You choose how many boxes it should be at the start, then you choose a number between 1 and total boxes.

A String array is created with the amount of total boxes and I fill up one randomly with "price" and the rest with "donkey".

I'm stuck with removing all arrays beside the one you choose and one that is either "donkey" or "price", which depends if you choose the "price/donkey" one.

For example:

You choose a total of 5 boxes.
You choose box number 2 as your box.

We will fill all those 5 boxes with (donkey/price); for example (donkey, donkey, price, donkey, donkey).

I need help with this.

In the example below, price is in box 3 and I choose box 2. I want a new String array to be created with box 3 and 2 in it. And then ask the person if he want to change box or not.

My code:

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        Random random = new Random();
        String price = "price";
        String donkey = "donkey";
        int boxes = 0;
        int choice = 0;
        int randomNumber ;
        String[] twoBoxes = new String[2];
        String[] totalBoxes = new String[boxes];

        System.out.print("How many boxes? (minimum 3): ");
        boxes = scanner.nextInt();
        randomNumber = random.nextInt(boxes);

        System.out.print("Choose a box between 1 and " + boxes + ": ");
        choice = scanner.nextInt();


        for (int i = 0; i < totalBoxes.length; i++) {

            if (randomNumber != i) {
                totalBoxes[i] = donkey;
            }
            else {
                totalBoxes[randomNumber] = price;
            }
        }

        System.out.println(Arrays.toString(totalBoxes));
    }
}



Aucun commentaire:

Enregistrer un commentaire