2

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

第一组给出了正确的输出,但我的第二个测试范围仅作为结果输出“太高”,因为它应该运行 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;
}
4

3 回答 3

4

您的问题是您的 else 条件总是在数字相等时触发,要求一个新数字。

基本上与以下相反:

if (guess < number)

是:

if (guess >= number)

您需要检查 >,而不是 >=

您还可以使用 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 测试中增加计数。

于 2013-10-13T23:49:11.110 回答
1

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

要在每次执行程序时使用不同的种子,srand(time(NULL));请在调用rand().

于 2013-10-13T23:46:50.443 回答
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;
}
于 2013-10-14T01:18:17.297 回答