利用C语言代码实现猜数字小游戏:
#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("退出游戏成功!!!\n");
break;
case PLAY:
case AGAIN:
printf("请输入您所猜的数字!\n");
count=guess();
End(count);
break;
default:
printf("输入有误,请输入菜单栏选项\n");
break;
}
} while (input!=0);
system("pause");
return 0;
}
void Print()
{
printf("*******祝您游戏愉快********\n");
printf("***************************\n");
printf("******1.开始游戏***********\n");
printf("***************************\n");
printf("******0.退出游戏***********\n");
printf("***************************\n");
printf("******2.再玩一次***********\n");
printf("***************************\n");
}
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("再猜猜?\n");
count++;
}
else
{
printf("猜大了!");
printf("再猜猜?\n");
count++;
}
}
return count+1;
}
void End(int count)
{
printf("恭喜您猜对了,总共猜了%d次\n", count);
printf("是否再玩一次?\n");
printf("***************************\n");
printf("******0.退出游戏***********\n");
printf("***************************\n");
printf("******2.再玩一次***********\n");
printf("***************************\n");
}
到此这篇关于C语言实现猜数字小游戏的文章就介绍到这了,更多相关C语言猜数字小游戏内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!