蒙蒂大厅展示模拟意想不到的结果

时间:2013-11-06 01:09:31

标签: c

#include <stdio.h>
#include <time.h>

int main (void)
{
    int pickedDoor, remainingDoor, hostDoor, winningDoor, option, games = 0, wins = 0;

    float frequency = 0;

    srand (time(NULL));


  while (1)
    {
        printf ("Pick one of the three doors infront of you, which do you want?\n");
        scanf ("%d", &pickedDoor);

        if (pickedDoor > 3 || pickedDoor <= 0)
        {
            break;
        }

        winningDoor = rand() % 3 + 1;


        do
        {
            hostDoor = rand() % 3 + 1;
        } while (hostDoor == pickedDoor || hostDoor == winningDoor);

        do
        {
            remainingDoor = rand() % 3+1;
        } while (remainingDoor == pickedDoor || remainingDoor == hostDoor);

        printf ("The door the host picked is %d\n", hostDoor);
        do
        {
        printf("Do you want to switch doors? Please enter in the door you want:\n", hostdoor);
        scanf("%d", &option);
       if (option > 3 || option <= 0)
         {return 0;}
        }while (option == hostDoor);


        if (option == winningDoor)
        {
            printf("You Won!\n");
            wins++;
        }

        else 
        {
            printf("YOU LOSE!\n");
        }

        games++;
    }

    frequency = ((float) wins / games) *100;

    printf ("The number of games that you won is %d\n", wins);

    printf ("The frequency of winning is %.0f%%\n", frequency);

    return 0;
}

嗨,这是我的monty hall游戏节目的版本,虽然我得到了意想不到的结果。

有时当我进入一扇门供我选择时,它只会让我回到“选择你面前的三扇门之一”的声明,当它应该告诉我是否赢了或输了。

我认为这是因为“选项”门等于“hostDoor。

我认为有“选项!= hostDoor”可以解决它,但事实并非如此。

如果我在这个假设中是正确的,我该如何解决?如果不是为什么它不起作用,我该如何解决?

3 个答案:

答案 0 :(得分:1)

因为这些:

scanf ("%d", &pickedDoor);// reads \n after the last input
scanf("%d", &option);     // reads \n after the last input

**option != hostDoor; // completely useless  .. get rid of it**

我建议在每个getchar()之后加scanf来摆脱\ n字符

这样的事情:

#include <stdio.h>
#include <time.h>

int main (void)
{
    int pickedDoor, remainingDoor, hostDoor, winningDoor, option, games = 0, wins = 0;
    char collect; //variable to collect any extra input like \n
    float frequency = 0;
    srand (time(NULL));

  while (1)
    {
        printf ("Pick one of the three doors infront of you, which do you want?\n");

        scanf ("%d", &pickedDoor);
        collect = getchar();  // get rid of the \n from the input stream
        printf("collect = %c\n",collect);
        if(collect!='\n'){    // is it actually a \n or did you take in something else
           putchar(collect);  // if it isn't \n put it back
        }

        if (pickedDoor > 3 || pickedDoor <= 0)
        {
            break;
        }

        winningDoor = rand() % 3 + 1;


        do
        {
            hostDoor = rand() % 3 + 1;
        } while (hostDoor == pickedDoor || hostDoor == winningDoor);

        do
        {
            remainingDoor = rand() % 3+1;
        } while (remainingDoor == pickedDoor || remainingDoor == hostDoor);

        printf("Do you want to switch doors? Please enter in the door you want:\n");
        scanf("%d", &option);
        collect = getchar();  // get rid of the \n from the input stream
        printf("collect = %c\n",collect);
        if(collect!='\n'){    // is it actually a \n or did you take in something else
           putchar(collect);  // if it isn't \n put it back
        }

        if (option > 3 || option <= 0 )
        {
            return 0;
        }

        if (option == winningDoor)
        {
            printf("You Won!\n");
            wins++;
        }

        else if (option == remainingDoor)
        {
            printf("YOU LOSE!\n");
        }

        games++;
    }
    frequency = ((float) wins / games) *100;
    printf ("The number of games that you won is %d\n", wins);
    printf ("The frequency of winning is %.0f%%\n", frequency);

    return 0;
}

另一种更有效的方法是使用fgets或对scanf()本身进行错误检查

答案 1 :(得分:1)

为了正确模拟,OP需要显示主机门。

do {
  printf("Do you want to switch doors? Please enter in the door you want:\n");
  scanf("%d", &option);
  if (option > 3 || option <= 0 ) {
    return 0;
    }
  } while (option == hostDoor);

// instead of 
#if 0
printf("Do you want to switch doors? Please enter in the door you want:\n");
scanf("%d", &option);
if (option > 3 || option <= 0 ) { return 0; }
#endif

处理OP“它应该告诉我我是赢了还是输了。”问题,改变

else if (option == remainingDoor)

else

您的scanf("%d", &option)没问题。我更喜欢fgets()/sscanf()组合,并且它总是很有用,可以查看scanf()系列的结果,但这不是您的问题。

答案 2 :(得分:0)

srand and rand属于stdlib头文件

#include<stdlib.h> // include this header file 

 option != hostDoor; // this statement does not do anything

,您的else if (option == remainingDoor)应为else { printf("you lose");}