#include <stdio.h> #include <stdlib.h> #include <time.h>int main () {int input = 1;while (Input) {printf ("***********************\n");p rintf ("*****>1. Start Game *******\n");p rintf ("* * * *>0. Exit Game *******\n ");p rintf (" ***********************\n ");p rintf (" Please enter directive (1 or 0): \ n "); scanf ("%d ", &input);switch (input) {case 1:{ int n = 0; int num = 0; srand (Time (NULL));//generates a random value based on timing n = rand () % 101; while (1) { printf ("Guess a number (1~100):"); &NBSP;&NBSP;&NBSP;SCANF ("%d", &num); if (num > n) { printf ("Guess big \"); } else if (num < n) { printf ("Guess small"); } else &nbSp; { printf (" Congratulations Right! \ n "); break; } } break;} default:{ printf ("Game quit!"); break;}}} System ("pause"); return 0;}
Improved, modular functions
#include <stdio.h> #include <stdlib.h> #include <time.h>void print_menu () {printf ("********** \ n ");p rintf (" *****>1. Start Game *******\n ");p rintf (" *****>0. Exit game *******\n ");p rintf (" ************** \ n ");p rintf (" Please enter directive (1 or 0): \ n ");} Int main () { print_menu ();int input = 1;while ( Input) {scanf ("%d", &input);switch (input) {case 1:{ print_menu (); int n = 0; int ch = 0; srand (Time (NULL));//Generate random values based on timing n = rand () % 101; while (1) { printf ("Guess a number (1~100):"); &NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%d", &ch); if (ch > n) { printf ("Guess Big"); } else if (ch < n) { printf ("Guess small"); } else { printf ("Congratulations right! "); break; } } break;} default:{ printf ("Game quit!"); break;}}} System ("pause"); return 0;}
This article is from the "incomparable Warm yang" blog, please be sure to keep this source http://10797127.blog.51cto.com/10787127/1707520
Guess number Game