对于C中的循环猜测游戏

时间:2013-10-22 03:36:23

标签: c

一个程序,让用户猜出程序选择的号码作为幸运号码。它使用一个for循环和大量的if语句。问题是我的代码在2次尝试后停止,但是假设给用户3次尝试。以下是我到目前为止的情况:

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

int main()
{
    int iSecret, iGuess;
    srand(time(NULL));
    iSecret = rand() % 20 + 1;
    int tries = 0;

    printf("\nWelcome to the number guessing game!\nFor each game, you have at most 3 chances to guess a secret number from 1 to 20.\n");

    for (tries = 0; tries < 3 || iSecret == iGuess; tries++)
    {
        printf("Please, enter a number between 1 and 20!   ");
        scanf("%d", &iGuess);

        if (iGuess == iSecret)
        {
            printf("\nCongratulations! You won!");
            return 0;
        }

        else if (iGuess > iSecret)
        {
            tries++;
            printf("\nYour guess was too high.\n");
        }

        else if (iGuess < iSecret)
        {
            tries++;
            printf("\nYour guess was too low.\n");
        }
        if (tries == 3)
            break;
    }

    printf("\nYou have reached your third trials. The correct number is %d.\n",
            iSecret);
    return 0;
}

5 个答案:

答案 0 :(得分:3)

您正在递增tries两次:一次在for定义中,以及稍后在循环体中。

删除额外的tries++语句。

答案 1 :(得分:0)

您可以在代码内部以及for语句中增加尝试次数。去掉if-blocks中的tries ++语句。

答案 2 :(得分:0)

在循环执行期间,您多次递增变量tries, 每次转弯,每次你没有猜到你的秘密

答案 3 :(得分:0)

  1. for循环已经递增尝试..你不需要在if语句
  2. 中执行++
  3. 你不需要||因为你已经在if语句
  4. 中进行检查,所以for循环中的条件

    这是固定代码:

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main ()
    {
      int iSecret, iGuess;
      srand ( time(NULL) );
      iSecret = rand() % 20 + 1;
      int tries = 0;
    
      printf("\nWelcome to the number guessing game!\nFor each game, you have at most 3 chances to guess a secret number from 1 to 20.\n");
    
    
      for (tries = 0; tries < 3 ; tries++) {
    
          printf ("Please, enter a number between 1 and 20!   ");
          scanf ("%d", &iGuess);
    
          if(iGuess == iSecret){
          printf ("\nCongratulations! You won!");
          return 0;
          }
    
            else if (iGuess > iSecret){
            printf ("\nYour guess was too high.\n");
            }
    
            else if (iGuess < iSecret){
                printf ("\nYour guess was too low.\n");
                      }
    
      }
      printf ("\nYou have reached your third trials. The correct number is %d.\n", iSecret);
    
      return 0;
      }
    

    <强>输出:

    $ ./test
    
    Welcome to the number guessing game!
    For each game, you have at most 3 chances to guess a secret number from 1 to 20.
    Please, enter a number between 1 and 20!   2
    
    Your guess was too low.
    Please, enter a number between 1 and 20!   3
    
    Your guess was too low.
    Please, enter a number between 1 and 20!   4
    
    Your guess was too low.
    
    You have reached your third trials. The correct number is 10.
    

答案 4 :(得分:0)

除了递增尝试次数太多,代码过于复杂,你可以简化逻辑,如

int main ()
{
  int iSecret, iGuess;
  srand ( time(NULL) );
  iSecret = rand() % 20 + 1;
  int tries = 0;

  printf("\nWelcome to the number guessing game!\nFor each game, you have at most 3 chances to guess a secret number from 1 to 20.\n");


  for (tries = 0; tries < 3; tries++) {
      printf ("Please, enter a number between 1 and 20!   ");
      scanf ("%d", &iGuess);

      if(iGuess == iSecret) {
        printf ("\nCongratulations! You won!");
        return 0;
      }

      printf ( "\nYour guess was too %s.\n", iGuess>iSecret?"high":"low");

  }
  printf ("\nYou have reached your third trials. The correct number is %d.\n", iSecret);
  return 0;
  }