猜测游戏的程序输出不正确

时间:2013-10-13 23:31:21

标签: c

我为猜谜游戏运行了两组测试范围, 1)1到100 和 2)50到90。

第一组给出正确的输出,但我的第二个测试范围仅输出“Too High”,因为它应该运行8个值太低,第九个太高,然后正确猜测在10日尝试。

这是我的代码,任何帮助都非常感谢,谢谢!

版本1

原始代码

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

int main()
{
    int min, max, number, guess, count = 0;

    printf("Please enter two integers for the range:\n");
    scanf("%d %d", &min, &max);

    number = (min + rand()%(max - min + 1));
    printf("I'm thinking of a number between %d and %d.\n", min, max);

    printf("Enter your guess:\n");
    scanf("%d", &guess);

    while(guess != number)
    {

        if(guess < number)
        {
            count++;
            printf("Too low.\n");
            printf("Enter your guess:\n");
            scanf("%d", &guess);
        }
        else
        {
            count++;
            printf("Too high.\n");
            printf("Enter your guess:\n");
            scanf("%d", &guess);
        }
    }

    count++;
    printf("Correct, it took you %d tries to guess my number. ", count);

    return 0;
}

第2版

根据答案的反馈修改代码

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

int main()
{
    int min, max, number, guess, count = 0;

    printf("Please enter two integers for the range:\n");
    scanf("%d %d", &min, &max);

    srand(time(NULL));
    number = (min + rand()%(max - min + 1));
    printf("I'm thinking of a number between %d and %d.\n", min, max);
    scanf("%d", &guess);

    do
    {
        printf("Enter your guess:\n");
        scanf("%d", &guess);

        if ( guess < number)
        {
            printf("Too low.\n");
        }
        else if ( guess > number)
        {
            printf("Too high.\n");
        }
    }
    while (guess != number && count++);

    count++;
    printf("Correct, it took you %d tries to guess my number. ", count);

    return 0;
}

3 个答案:

答案 0 :(得分:4)

您的问题是,当数字相等时,您的其他条件总会触发,请求新号码。

基本上与之相反:

if (guess < number)

是:

if (guess >= number)

您需要检查&gt;,而不是&gt; =

您还可以使用do while循环来减少代码。

do {
    count++;  
    printf("Enter your guess:\n");
        scanf("%d", &guess);
      if ( guess < number) 
        printf("Too low.\n");
     else if ( guess > number)
          printf("Too high.\n");
   }while (guess != number);

编辑:不应该在while测试中放入计数增量。

答案 1 :(得分:1)

rand()总是返回相同的数字(或者如果你在程序执行中多次调用它,则返回数字序列)因为它总是使用相同的种子数来生成随机数。这导致猜测数字总是对于给定的测试用例是相同的。

要在每次执行程序时使用不同的种子,请在致电srand(time(NULL));之前添加行rand()

答案 2 :(得分:1)

这里有一些适合我的代码。目前,出于调试目的,它打印出“猜测x和y之间的数字”行中的数字(这在早期阶段可能会有所帮助)。它还会测试您的许多错误情况。 “超出范围”的消息是可选的; “太低”和“太高”的消息确实传达了正确的信息。请注意,代码回应输入的信息 - 尝试调试错误的输入(您认为您输入了A但程序认为您输入了B)可能导致无休止的调试混乱。

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

static int get_guess(int *guess)
{
    printf("Enter your guess: ");
    if (scanf("%d", guess) != 1)
        return EOF;
    return 0;
}

int main(void)
{
    int min, max, number, guess, count = 0;
    srand(time(0));

    printf("Please the minimum and maximum values for the range: ");
    if (scanf("%d %d", &min, &max) != 2)
    {
        printf("Oops!\n");
        return 1;
    }
    if (min <= max)
    {
        printf("Your minimum (%d) is not smaller than your maximum (%d)\n", min, max);
        return 1;
    }

    number = (min + rand()%(max - min + 1));
    printf("I'm thinking of a number between %d and %d. (%d)\n", min, max, number);

    while (count < (max - min + 1) && get_guess(&guess) != EOF)
    {
        count++;
        if (guess < min)
            printf("%d: Your guess is smaller than the minimum (%d)\n", guess, min);
        else if (guess > max)
            printf("%d: Your guess is larger than the maximum (%d)\n", guess, max);
        else if (guess < number)
            printf("%d: Too low.\n", guess);
        else if (guess > number)
            printf("%d: Too high.\n", guess);
        else
        {
            printf("%d: Correct, it took you %d tries to guess my number.\n", guess, count);
            break;
        }
    }
    if (guess != number && count >= (max - min))
        printf("You didn't guess the number (%d) after %d tries.\n", number, count);

    return 0;
}