mercredi 7 octobre 2015

Monty Hall lets make a deal in JAVA using simpler operators and functions

So I've been working on this assignment for about 12 hours and have been unsuccessful in covering all the parameters. I was asked to make a program based off of Monty Hall "Lets make a deal" and asked to check each user input for validity up to the switching of doors;

enter image description here

I'm having the following issues:

  • if the user wants to switch doors after a zonk is revealed they're taken back to the main menu where they're asked to pick a door. then put in a continuous input loop

  • if the user's input is invalid at the switch doors scenario then the same problem as above happens

  • problem displaying the win percentage

  • problem when wanting to play the game again

please help, somewhat of a beginner so criticism is more than welcomed on all parts of the code.

    System.out.println("WELCOME TO 'LETS MAKE A DEAL'");
    System.out.println("Please Enter 'A' to Play, 'B' To Watch, or 'Q' To Quit");
    Scanner input = new Scanner (System.in);
    String choice = input.next();

    boolean done = false;   
    double wins = 0;
    double loses = 0;       
    double games = 0;

    while (!done)
    {

        if(choice.equals("A"))
        {   
            System.out.println("Please Choose a door:\n");
            System.out.println("[1] [2] [3]\n");
            System.out.println("Type '1', '2', or '3'");

            if(input.hasNextInt())
            {   
                int chosenDoor = input.nextInt();
                if(chosenDoor <= 3 && chosenDoor > 0)
                {

                    int prizeIs = (int) ((Math.random() * 3) + 1);      
                    int finChoice = 0;
                    int zonkIs = 0;


                        while (prizeIs == chosenDoor)
                        {
                            zonkIs = (int) ((Math.random() * 3) + 1);
                            while (zonkIs == prizeIs)
                            {
                                zonkIs = (int) ((Math.random() * 3) + 1);
                            }
                        }
                        if (prizeIs == 1 && chosenDoor == 2)
                        {
                            zonkIs = 3; 
                        }
                        else if (prizeIs == 1 && chosenDoor == 3 )
                        {
                            zonkIs = 2; 
                        }
                        else if (prizeIs == 2 && chosenDoor == 1 )
                        {
                            zonkIs = 3;
                        }
                        else if (prizeIs == 2 && chosenDoor == 3 )
                        {
                            zonkIs = 1;
                        }
                        else if (prizeIs == 3 && chosenDoor == 1 )
                        {
                            zonkIs = 2;
                        }
                        else if (prizeIs == 3 && chosenDoor == 2 )
                        {
                            zonkIs = 1;
                        }

                        System.out.println("\nI Will Now Reveal A Zonk\n\nDoor [" + zonkIs + "]");
                        System.out.println("\nKnowing This, Would You Like To Switch Doors? ('Y' or 'N') ");

                        String decision = input.next();

                        if(decision.equals("Y"))
                        {
                            System.out.println("Pick A New Door (Not The One With A Zonk)");
                            chosenDoor = input.nextInt();
                            finChoice = chosenDoor;
                            System.out.println("\nWell Then\n\nThe Moment You've Been Waiting For\n");
                            System.out.println("The Prize is in\n\nDoor [" + prizeIs + "]");
                            if (prizeIs == finChoice || prizeIs == chosenDoor)
                            {
                                System.out.println("\nCONGRATUALTIONS!!!\nYOU WON!!!!!");
                                wins++;
                                games++;
                            }
                            else 
                            {
                                System.out.println("\n..Sorry, You Lost.");
                                loses++;
                                games++;
                            }

                            System.out.println("\nWould You Like To Play Again? ('Y' or 'N')");
                            decision = input.next();

                            if(decision.equals("N"))
                            {
                                System.out.println("\nWell Thanks For Playing\nYour Win Percentage was ");
                                if(wins <= 0.0 || wins < loses)
                                {
                                    double percentage = 0;
                                    System.out.printf(percentage +"%");
                                }
                                else
                                {
                                    double percentage = (wins-loses)/games * 100;
                                    System.out.printf("%5.2f", percentage +"%");
                                }   
                                done = true;
                                input.close();
                            }
                            else if(decision.equals("Y"))
                            {

                                System.out.println("*******************************");

                            }
                            else
                            {
                                System.out.println("Invalid Entry, Please Try Again ('Y' or 'N')");

                            }

                        }
                        else if(decision.equals("N"))
                        {
                            finChoice = chosenDoor;
                            System.out.println("\nWell Then\n\nThe Moment You've Been Waiting For\n");
                            System.out.println("The Prize is in\n\nDoor [" + prizeIs + "]");
                            if (prizeIs == finChoice || prizeIs == chosenDoor)
                            {
                                System.out.println("\nCONGRATUALTIONS!!!\nYOU WON!!!!!");
                                wins++;
                                games++;
                            }
                            else 
                            {
                                System.out.println("\n..Sorry, You Lost.");
                                loses++;
                                games++;
                            }

                            System.out.println("\nWould You Like To Play Again? ('Y' or 'N')");
                            decision = input.next();

                            if(decision.equals("N"))
                            {
                                System.out.println("\nWell Thanks For Playing\nYour Win Percentage was ");
                                if(wins <= 0.0 || wins < loses)
                                {
                                    double percentage = 0;
                                    System.out.printf(percentage +"%");
                                }
                                else
                                {
                                    double percentage = (wins-loses)/games * 100;
                                    System.out.printf("%5.2f", percentage +"%");
                                }   
                                done = true;
                                input.close();
                            }
                            else if(decision.equals("Y"))
                            {

                                System.out.println("*******************************");

                            }
                            else
                            {
                                System.out.println("Invalid Entry, Please Try Again ('Y' or 'N')");

                            }

                        }
                        else 
                        {
                            System.out.println("Invalid Entry, Please Try Again ('Y' or 'N')");

                        }


                    }
                else
                {
                    System.out.println("Invalid Entry, Please Try Again.");
                }

                }
                else
                {
                    System.out.println("Invalid Entry, Please Try Again.");
                    input.next();
                }
            }

            else if(choice.equals("B"))
            {

            }
            else if(choice.equals("Q"))
            {
                done = true;
                input.close();
            }
            else
            {
                System.out.println("Invalid Entry, Please Try Again..");
                choice = input.next();
            }
    }
    }
}

Aucun commentaire:

Enregistrer un commentaire