猜数字
来源:互联网 发布:中式服装 知乎 编辑:程序博客网 时间:2024/06/05 17:46
猜数字,计算机随机给出一个数字,玩家一直猜,直到猜出计算机给出的数字,游戏结束。
#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include <stdlib.h>#include <time.h>void menu(){ printf("**************************************************\n"); printf("********* 1.play ************\n"); printf("********* 0.exit ************\n"); printf("**************************************************\n");}void game(){ int input = 0; int num = rand() % 201;//生成1—200之间的随机数 while (1) { printf("请猜数字:>\n"); scanf("%d", &input); if (input == num) { printf("恭喜你,猜对了\n"); break; } else if (input>num) { printf("对不起,猜大了:>\n"); } else { printf("对不起,猜小了:>\n"); } }}int main(){ int input = 0; srand((unsigned int)time(NULL)); do { menu(); printf("请选择;>\n"); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: break; default: printf("输入错误\n"); break; } } while (input); system("pause"); return 0;}