我运行一个随机数生成器,但外循环只运行一次

时间:2018-02-04 17:40:20

标签: c

这就是我现在所处的位置,我查找的代码与此类似,但它们似乎都有不同的东西使它们分开。我觉得我的错误就在我面前,但是我太盲目无法看到它。

这是我的代码:

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

int main()
{
    srand(time(NULL));

    int rnum = 1 + rand() % 1000;
    int guess = 0;
    char answer;

    printf("Would you like to play (y or n)?\n");
    scanf_s("%c", &answer);

    while (answer == 'y')
    {
        printf("I have a number between 1 and 1000.\n");
        printf("Can you gueess my number?\n");
        printf("Please type your first guess.\n");
        scanf_s("%d", &guess);

        while (rnum != guess)
        {
            if (rnum > guess)
            {
                printf("Too low. Try again.\n");
                scanf_s("%d", &guess);
            }
            else
            {
                printf("Too high. Try again.\n");
                scanf_s("%d", &guess);
            }
        }
        printf("Execellent! You guess the number!\n");
        printf("Would you like to play again (y or n)?\n");
        scanf_s("%c", &answer);
    }
    return 0;
}

0 个答案:

没有答案