Exercise: guess the number game (knowledge points: timestamp, random number)

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<time.h>                                                      //The header file required to call the timestamp function
#include<stdlib.h>                                                     //The header file required to call rand to generate random numbers 
void menu()
{     printf("##############################\n");     printf("#######1, game 0. exit#####\n");          // Create game menu function     printf("############################# #\n"); } void game() {     int x = 0;     int ret = 0;     ret = rand()%100+1;                                                       //Generate a random number between 1 and 100     while (1)     {











        printf("Please guess the number, the number is between 1 and 100\n");
        scanf("%d", &x);                                                          //Enter the number
        if (x > ret)                                                                     //Determine whether the input number is greater than the generated random number
        {             printf("guess too big\n");         }         else if (x < ret)         {             printf("guess too small\n");         }         else                                                                               //Determine that the input number is equal to the generated random number         {             printf("Congratulations, You guessed it\n");             break;         }     } } int main() {















    srand((unsigned int)time(NULL));                                    //use timestamp to generate random value
    int a = 0;
    do
    {         menu();         printf("Please choose:\n");         scanf("%d", &a );         switch (a)         {                                                                                    //Create a branch statement, divided into three cases         case 1:             game();             break;         case 0:             printf("Exit the game\n");             break;          default:             printf("Input error\ n");             break;         }     } while (a);     return 0; }

















The result of running the code is as follows:

 


 

Guess you like

Origin blog.csdn.net/JXS88/article/details/123777165