1
#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”可以解决它,但事实并非如此。

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

4

3 回答 3

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()自身进行错误检查

于 2013-11-06T01:13:10.900 回答
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()家庭的结果总是有用的,但这不是你的问题。

于 2013-11-06T01:42:40.017 回答
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");}

于 2013-11-06T02:09:33.810 回答