⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 program4_07a.c

📁 C语言入门经典一书的所有代码。书上面的所有代码均在此。希望大家喜欢
💻 C
字号:
/* Program 4.7A  A More Interesting Guessing Game */
#include <stdio.h>
#include <stdlib.h>                    /* For rand() and srand() */
#include <time.h>                      /* For time() function    */

int main(void)
{
  int chosen = 0;                     /* The lucky number                     */
  int guess = 0;                        /* Stores a guess                         */
  int count = 3;                        /* The maximum number of tries            */
  int limit = 20;                     /* Upper limit for pseudo-random values */

  srand(time(NULL));                  /* Use clock value as starting seed     */
  chosen = 1 + rand()%limit;          /* Random int 1 to limit                */

  printf("\nThis is a guessing game.");
  printf("\nI have chosen a number between 1 and 20"
                                       " which you must guess.\n");

  for( ; count>0 ; --count)
  {
    printf("\nYou have %d tr%s left.", count, count == 1 ? "y" : "ies");
    printf("\nEnter a guess: ");      /* Prompt for a guess  */
    scanf("%d", &guess);              /* Read in a guess     */

    /* Check for a correct guess */
    if (guess == chosen)
    {
      printf("\nYou guessed it!\n");
      return 0;                       /* End the program */
    }

    /* Check for an invalid guess */
    if(guess<1 || guess > 20)
      printf("I said between 1 and 20.\n ");
    else
      printf("Sorry. %d is wrong.\n", guess);
  }
   printf("\nYou have had three tries and failed. The number was %ld\n",
                                                                 chosen);
  return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -