Monty Hall让我们可以使用更简单的运算符和函数在JAVA中进行交易

时间:2015-10-07 10:03:50

标签: java if-statement

因此,我已经在这项任务中工作了大约12个小时,并且未能成功覆盖所有参数。我被要求制作一个基于Monty Hall&#34的节目;让我们做一笔交易"并要求检查每个用户输入的有效性,直到切换门;

enter image description here

我遇到以下问题:

  • 如果用户想要在显示zonk后切换门,他们会被带回主菜单,他们要求他们选择一扇门。然后放入一个连续的输入循环

  • 如果用户的输入在开关门方案中无效,则会出现与上述相同的问题

  • 显示胜率的问题

  • 想要再次玩游戏的问题

请帮助,有点像初学者,所以批评在代码的所有部分都受到欢迎。

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

1 个答案:

答案 0 :(得分:3)

首先,这部分代码毫无意义:

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

这里的外部while循环,因为你既不改变里面的prizeIs也不改变chosenDoor,这将是一个无限循环。

此外,从三个门中选择zonks没有意义,因为在我们有prizeIs之后,只有两个zonks,这是其他门。我想,最好使用集合或数组shuffle,但如果你不被允许,你可以列出可能性。

if ( prizeIs == chosenDoor ) { // Note it's an if, not a while.
    boolean chooseFirstZonk = Math.random() < 0.5; // 50% chance
    switch ( prizeIs ) {
        case 1:
            if ( chooseFirstZonk ) {
                 zonkIs = 2;
            } else {
                 zonkIs = 3;
            }
            break;
        case 2:
            if ( chooseFirstZonk ) {
                 zonkIs = 1;
            } else {
                 zonkIs = 3;
            }
            break;
        case 3:
            if ( chooseFirstZonk ) {
                 zonkIs = 1;
            } else {
                 zonkIs = 2;
            }
            break;
    }
}

然后这个if

if (prizeIs == 1 && chosenDoor == 2)

成为上述else if的{​​{1}}。

接下来,你对游戏有一点误解。现在zonk已被揭示,只有两扇门被覆盖。如果用户选择切换,他不应该从三个中选择一个新门。人们都知道有一个人是zonk,而且他知道有一个人选择放弃他的先前选择。因此,当用户想要切换时,您应该只需将if更改为未显示的门。

  • 如果原来选择的门是奖品门,您应该将chosenDoor切换到第二个zonk。因此,如果prizeIs为1,chosenDoorchosenDoorzonkIs为2,则更改chosenDoor = 3。如果zonkIs为3,则执行chosenDoor = 2
  • 如果原来选择的门不是奖品门,您应该分配chosenDoor = prizeIs - 当用户选择了zonk时,您显示了另一个zonk,因此剩下的唯一一个是奖品门。< / LI>

因此,在这种情况下不需要用户输入。

所以你需要改变那个大的if-else。如果用户选择切换,则执行计算。这个if没有else,因为当用户没有说是,他意味着保留原来的选择。此时,检查是否chosenDoor == prizeIs,并计算百分比。

<强>计算

首先,您只需要保留两个变量。喜欢输赢,或胜利和比赛,或损失和比赛。你可以随时计算损失作为游戏 - 获胜。

所以始终games++,不要做任何损失,并在玩家获胜时做wins++

现在计算成功率不需要那些ifwins不能小于零。它也不会比games更伟大。

但重要的是要记住,如果将整数除以整数,您将获得整数除法。也就是说,5/10为您提供,而不是0.5,因为它不是整数。

因此,在分割之前将其中一个数字转换为双数非常重要。一种简单的方法是将100更改为100.0,并将其移至开头:

double percentage = 100.0 * wins / games;

这样,100.0 * wins会自动将wins的值转换为double。因此,当结果除以games时,games的值也会转换为double,并且没有整数除法。