- First introduce the game
Guess the number: the computer randomly generates a number num, and the player guesses it. If the guessed number is larger than the num generated by the system, it will prompt "guess too big", if the guessed number is smaller than the number generated by the system, it will prompt "guess small ", until the position of the generated number is guessed, it prompts "Congratulations! You guessed it!".
- Next, let's show the effect we want to achieve
Referring to the picture above, we summarize the following questions
- First we need a menu bar
- Enter 1 or 0 to choose to enter or exit the game, and you need to return to the menu bar page after the game is over
- Generate a random number from 1-100
- After entering the game, compare the number guessed by the player with the number generated by the system and make a judgment
Answer questions one by one
void menu() { printf("********************************************\n"); printf("**********1. open make game ************\n"); printf("**********0.. retreat out *************\n"); printf("********************************************\n"); }
We choose to print directly in the menu bar, and wrap it in a function to make the code more readable
do { menu(); printf("Please select (1 or 0): "); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: break;//If input is 0, end the do while statement directly default: printf("Input errors, please re-enter\n"); } } while (input);
The user selects 0 or 1 to execute exit or enter the game. We choose the switch case statement. If you enter the game, the menu bar will continue to pop up after the game is over, so we choose to add a do whlie statement outside.
srand((unsigned int)time(NULL)); int num = rand()/100+1;
To generate random numbers, we choose the rand function. To generate the rand function, we need to use the srand function first. A random value needs to be placed in the srand function. The common random value in our life is time, so we put the timestamp function in, and then the rand function %100+1 so that the random number we generate is 1-100.
int num = rand()%100+1; int guess = 0; printf("Guess 1-100 numbers in\n"); while (1) { scanf("%d", &guess); if (num > guess) { printf("guess small\n"); } else if (num < guess) { printf("guess big\n"); } else { printf("Congratulations, you guessed it!\n"); break; } }
Compare the size of the number with the random number, and prompt whether the guess is big or small, until the guess is correct. For the judgment, we choose the if else statement, and we cannot exit until the guess is right, so we choose '1' in the loop and continue to loop until the guess is right and break is encountered.
- Integrate the four parts
#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h>//Header files required for input and output library functions #include <time.h>//The header file required by the timestamp library function #include <stdlib.h>//The header files needed to generate random number library functions //guess the number game void game() { int num = rand()%100+1; int guess = 0; printf("Guess 1-100 numbers in\n"); while (1) { scanf("%d", &guess); if (num > guess) { printf("guess small\n"); } else if (num < guess) { printf("guess big\n"); } else { printf("congratulations you guessed it right\n"); break; } } } void menu() { printf("********************************************\n"); printf("**********1. open make game ************\n"); printf("**********0.. retreat out *************\n"); printf("********************************************\n"); } int main() { srand((unsigned int)time(NULL));//The library function stipulates that the value inside must be unsigned int, so the mandatory type conversion int input = 0; do { menu(); printf("Please select (1 or 0): "); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: break; default: printf("Input errors, please re-enter\n"); } } while (input);//Enter 0 to exit, input is false, exit the loop to end the program }
This is the whole process of guessing the number game! If you think the author's writing is good, you can like it and follow it. I wish you all a happy new year~~! !