1

一个程序,让用户猜测程序选择作为幸运数字的数字。它使用一个 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;
}
4

5 回答 5

3

您要增加tries两次:一次在for定义中,然后在循环体中。

删除多余的tries++语句。

于 2013-10-22T03:38:20.373 回答
0

您可以在代码中以及 for 语句中增加尝试次数。去掉 if 块中的 Try++ 语句。

于 2013-10-22T03:39:00.897 回答
0
  1. for 循环已经增加了尝试 .. 你不需要在 if 语句中做 try++
  2. 你不需要 || for 循环中的条件,因为您已经在检查 if 语句

这是固定代码:

#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.
于 2013-10-22T03:42:09.787 回答
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;
  }
于 2013-10-22T03:43:21.463 回答
0

tries在循环执行期间多次增加变量,每转一次,每次您没有猜对您的秘密

于 2013-10-22T03:40:46.347 回答