Simulates guessing a number game, prompting the user to enter a range of digits each time.
#include <stdio.h> #include <stdlib.h>//guess number game int main () {Int i=0;int num =0,ret=0,choose=0;int min=1,max=100;flag:srand ((unsigned) time (NULL)), ret=rand ()%100+1;while (1 {printf ("Please enter a number between%d~%d: \ n", Min,max), scanf ("%d", &num); if (num< RET) { min=num; printf ("Guess small, between%d~%d \ n", Min,max); } if (Num>ret) { max=num; printf ("Guess big, between%d~%d \ n", Min,max); } if (Num==ret) { printf ("Guess right, do you want to continue?") \n1. is 2. No \ n "); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%d ", &choose); switch ( Choose) { case 1: min=1; max=100; goto flag; break; case 2: exit (0); break; } }}return 0;}
This focus is on the generation of random numbers, and the random number produced is different each time, using Ret=srand ((unsigned) (NULL))
Note that header files should be added #include<stdlib.h>
This article from "10954937" blog, declined reprint!
Programming growth diary ———— guessing number games