C 猜谜游戏
C語言實現猜謎游戲
利用C語言的判斷循環,以及獲取值實現猜謎游戲,用戶根據提示輸入任意數據,程序會獲取你輸入的數據并判斷大小給出提示,用戶再次輸入數據經過若干次循環判斷使用戶猜出正確的答案。
完整代碼
#include <stdio.h> #include <stdlib.h> #include <time.h>void caizi(void) {int n;char begin;int count = 1;srand((int)time(NULL));int m = (rand() % 100) + 1;puts("游戲開始,請輸入數字:");while (1){scanf("%d", &n);if (n == m){printf("猜中了,使用了 %d 次!\n", count);if (count == 1){printf("你是神級人物了!膜拜\n");getchar();printf("你已經達到最高級別,還需要玩嗎?Y/N \n");scanf("%c", &begin);if (begin == 'Y' || begin == 'y') //重復玩的一個嵌套循環{caizi();}else{printf("謝謝,再見!\n");}}else if (count <= 5){printf("你是王級人物了!非常贊\n");getchar();printf("需要挑戰最高級別不?Y/N \n");scanf("%c", &begin);if (begin == 'Y' || begin == 'y'){caizi();}else{printf("謝謝,再見!\n");}}else if (count <= 10){printf("你是大師級人物了!狂贊\n");getchar();printf("需要挑戰最高級別不?Y/N \n");scanf("%c", &begin);if (begin == 'Y' || begin == 'y'){caizi();}else{printf("謝謝,再見!\n");}}else if (count <= 15){printf("你是鉆石級人物了!怒贊\n");getchar();printf("需要挑戰最高級別不?Y/N \n");scanf("%c", &begin);if (begin == 'Y' || begin == 'y'){caizi();}else{printf("謝謝,再見!\n");}}else{getchar();printf("你的技術還有待提高哦!重玩? Y/N\n");scanf("%c",&begin);if (begin == 'Y' || begin == 'y'){caizi();}else{printf("謝謝,再見!\n");}}break;}else if (n < m){puts("太小了!");puts("重新輸入:");}else{puts("太大了!");puts("重新輸入:");}count++;//計數器} }int main(void) {caizi();system("pause");return 0; }總結
- 上一篇: C++ 双向队列
- 下一篇: STL中“大”、“小”和“相等”的概念