1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 4 #include <stdlib.h> 5 #include <time.h> 6 void menu() 7 { 8 printf("********************************** "); 9 printf("*********** 1.play*************** "); 10 printf("*********** 0.exit*************** "); 11 printf("********************************** "); 12 } 15 void game() 16 { 17 int random_num = rand() % 100 + 1; 18 int input = 0; 19 while (1) 20 { 21 printf("请输入猜的数字>:"); 22 scanf("%d", &input); 23 if (input > random_num) 24 { 25 printf("猜大了 "); 26 } 27 else if (input < random_num) 28 { 29 printf("猜小了 "); 30 } 31 else 32 { 33 printf("恭喜你,猜对了 "); 34 break; 35 } 36 } 37 } 38 int main() 39 { 40 int input = 0; 41 srand((unsigned)time(NULL)); 42 do 43 { 44 menu(); 45 printf("请选择"); 46 scanf("%d", &input); 47 switch (input) 48 { 49 case 1: 50 game(); 51 break; 52 case 0: 53 break; 54 default: 55 printf("选择错误,请重新输入! "); 56 break; 57 } 58 } while (input); 59 return 0; 60 }
//可实现反复游戏
//用srand((unsigned)time(NULL))设置时间种子,从而使得系统每次产生预猜的数字不一样