C语言练习2---猜数字小游戏
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
-
- void Print();
- int guess();
- void End(int count);
-
- enum chonice
- {
- EXIT,
- PLAY,
- AGAIN
- };
-
- int main()
- {
- srand((unsigned)time(NULL));
- int input = 0;
- int count=0;
- Print();
- do
- {
- scanf("%%%%d", &input);
- switch (input)
- {
-
- case EXIT:
- printf("退出游戏成功!!!
");
- break;
- case PLAY:
- case AGAIN:
- printf("请输入您所猜的数字!
");
- count=guess();
- End(count);
- break;
- default:
- printf("输入有误,请输入菜单栏选项
");
- break;
- }
-
- } while (input!=0);
- system("pause");
- return 0;
- }
-
- void Print()
- {
- printf("*******祝您游戏愉快********
");
- printf("***************************
");
- printf("******1.开始游戏***********
");
- printf("***************************
");
- printf("******0.退出游戏***********
");
- printf("***************************
");
- printf("******2.再玩一次***********
");
- printf("***************************
");
- }
-
- int guess()
- {
- int a = 0;
- int count = 0;
- int b = rand() %%%% 100 + 1;
- while (scanf("%%%%d", &a), b != a)
- {
- if (a <= (a + b) / 2)
- {
- printf("猜小了!");
- printf("再猜猜?
");
- count++;
- }
- else
- {
- printf("猜大了!");
- printf("再猜猜?
");
- count++;
- }
- }
-
- return count+1;
- }
-
- void End(int count)
- {
- printf("恭喜您猜对了,总共猜了%%%%d次
", count);
- printf("是否再玩一次?
");
- printf("***************************
");
- printf("******0.退出游戏***********
");
- printf("***************************
");
- printf("******2.再玩一次***********
");
- printf("***************************
");
- }
-
推荐阅读