int i, j, k; int num = 0; /* Generate random numbers */ num = rand (); printf("%d\n", num);
Choose to play the game or quit
void play(int x) { printf("%d\n", x); printf("Start the game"); } scanf("%d", &k); switch (k) { case 1: play(num); case 2: break; }
loop game program
void play(int x) { printf("%d\n", x); printf("Start the game"); printf("Game over"); } intmain() { int i, j, k; int num = 0; /* Generate random numbers */ num = rand (); printf("%d\n", num); again: { scanf("%d", &k); switch (k) { case 1: play(num); goto again; case 2: break; } }
Write and play the internal program of the game
void play(int x) { int number=0; printf("Please enter a number\n"); again_1: { scanf("%d", &number); while (number != x) { if (number > x) { printf("Big"); goto again_1; } else { printf("Small"); goto again_1; } }printf("Right\nPlease select\n\n"); } }
At this point, the overall framework of the game has been compiled and can run normally. But we found that every time the number is 41 (or other fixed number)
/* *** Guess the number game */ void play(int x) { int number=0; printf("Please enter a number\n"); again_1: { scanf("%d", &number); while (number != x) { if (number > x) { printf("Big"); goto again_1; } else { printf("Small"); goto again_1; } }printf("Right\nPlease select\n\n"); } } intmain() { int i, j, k; int num = 0; /* Generate random numbers */ num = rand (); again: { printf("Start game\n1.Start\n2.Exit\n"); scanf("%d", &k); switch (k) { case 1: play(num); goto again; case 2: break; } } system("pause"); return 0; }
About the <time.h> header file
generate random numbers
/* Generate random numbers */ // num = rand (); srand(time(NULL)); num = rand ()% 100
final program
#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include <stdlib.h> #include <time.h> /* *** Guess the number game */ void play(int x) { int number=0; printf("Please enter a number\n"); again_1: { scanf("%d", &number); while (number != x) { if (number > x) { printf("Big\n"); goto again_1; } else { printf("Small\n"); goto again_1; } }printf("\n***Right***\n *********\n ******\n ****\n **\n"); } } intmain() { int k; int num = 0; /* Generate random numbers */ // num = rand (); srand(time(NULL)); num = rand ()% 100; again: { printf("Start game\n1.Start\n2.Exit\n"); scanf("%d", &k); switch (k) { case 1: play(num); goto again; case 2: break; } } system("pause"); return 0; }
Detailed introduction to the analysis of randomly generated numbers
https://mp.csdn.net/postedit/79788815