幸運52(猜數字遊戲)
阿新 • • 發佈:2018-12-09
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("**********************************\n"); 9 printf("*********** 1.play***************\n"); 10 printf("*********** 0.exit***************\n"); 11 printf("**********************************\n"); 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("猜大了\n"); 26 } 27 else if (input < random_num) 28 { 29 printf("猜小了\n"); 30 } 31 else 32 { 33 printf("恭喜你,猜對了\n"); 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("選擇錯誤,請重新輸入!\n"); 56 break; 57 } 58 } while (input); 59 return 0; 60 }
//可實現反覆遊戲
//用srand((unsigned)time(NULL))設定時間種子,從而使得系統每次產生預猜的數字不一樣