C語言編程 如何構建一個簡單的猜數字小遊戲
阿新 • • 發佈:2019-03-26
term 51cto 界面 如何 技術 pause 函數 vpd clu 源代碼如下:
#include<stdio.h> #include<stdlib.h> #include<time.h>//生成隨機函數起點時用到time.h int main() { int number1 = 0, choice = 0, number2 = 0; printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");//界面設置 printf("$$$$$ 歡 迎 來 到 $$$$$\n"); printf("$$$$$ 猜 數 字 遊 戲 $$$$$\n"); printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n"); printf(" 1.進入遊戲\n"); printf(" 2.退出遊戲\n"); scanf("%d", &choice); srand((unsigned int)time(NULL));//設置隨機數起點 while (choice == 1) { printf("請輸入所猜的數:\n"); scanf("%d", &number2); number1 = rand() % 100; while (1) { if (number2 == number1) { printf("666 恭喜你答對了!"); printf("\n1.再玩一次\n2.退出遊戲\n"); scanf("%d", &number2); if (number2 == 1) break; if (number2 == 2) return 0; } if (number2 < number1) { printf("\n猜小了!\n"); scanf("%d", &number2); } if (number2 > number1) { printf("\n猜大了!\n"); scanf("%d", &number2); } } } while(choice == 2) { return 0; } system("pause"); return 0; }
運行結果:
C語言編程 如何構建一個簡單的猜數字小遊戲