# C语言实现猜数字小项目

## 2.项目实现

```void menu()
{
printf("********************\n");
printf("***** 1.bYtRNDplay *******\n");
printf("***** 0.exit *******\n");
printf("********************\n");
}```

```void game()
{
int ret = rand() % 100 + 1;//为了产生1到100的随机数
int guess = 0;http://www.cppcns.com
while (1)
{
printf("请输入->");
scanf("%d", &guess);
printf("\n");
if (guess == ret)
{
printf("太棒了，你猜对了！！！\n\n");http://www.cppcns.com
break;
}
else if (guess < ret)
{
printf("猜小了！！！\n\n");
}
else
{
printf("猜大了！！！\n\n");
}
}
}```

## 3.完整代码如下

```#include<stdio.h>
#include<time.h>
{
printf("********************\n");
printf("***** 1.play *******\n");
printf("***** 0.exit *******\n");
printf("********************\n");
}
void game()
{
int ret = rand() % 100 + 1;//产生1到100随机数
int guess = 0;
while (1)
{
printf("请输入->");
scanf("%d", &guess);
printf("\n");
if (guess == ret)
{
printf("太棒了，你猜对了！！！\n\n");
break;
}
else if (guess < ret)
{
printf("猜小了！！！\n\n");
}
else
{
printf("猜大了！！！\n\n");
}
}
}
int main()
{
int input=0;

srand((unsigned int)time(NULL));//导入随机种子

do
{
printf("请选择->");
scanf("%d", &input);
printf("\n");
swithttp://www.cppcns.comch (input)
{
case 1:
printf("开始游戏\n\n");
game();
break;
case 0:
printf("退出游戏");
break;
default:
printf("输入错误，请重新输入！！！\n");
}

} while (input);http://www.cppcns.com
return 0;
}
```