srand((unsigned int)time(NULL));
int q = rand()%100+1;
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void qw() {
//猜数字游戏
srand((unsigned int)time(NULL));
//使用随机数的生成器srand()
//time函数的时候需要包含头⽂件:time.h
//time函数会返回当前的⽇历时间设置种⼦,NULL就是系统定义特殊的0.
//因为srand的参数是unsigned int类型,我们将time函数的返回值强制类型转换
int q = rand()%100+1;//控制在100百内
//rand函数的使⽤需要包含⼀个头⽂件是:stdlib.h
int w = 0;
int r = 5;//设置5次机会。
while (1) {
printf("请猜数字>:");
printf("你还有%d次机会\n",r);
scanf_s("%d",&w);
if (w < q) {
printf("猜小了\n");
r--;//输入错误机会减一次
}else if (w > q) {
printf("猜大了\n");
r--;
}
else {
printf("恭喜你,猜对了\n");
break;
}
if (r == 0) {
printf("机会用完,游戏结束");
break;
}
}
}
void wq() {
printf("*********************\n");
printf("***** 1.游戏 *********\n");
printf("***** 2.离开 *********\n");
printf("*********************\n");
}
int main() {
int e;
do {
wq();//调用wq函数作为开始界面
printf("请选择:>");
scanf_s("%d", &e);
switch (e) {
case 1:
qw();//选择一后进入pw函数
case 2:
printf("游戏结束\n");
break;
default:
printf("输入错误\n");
}
} while (e);
return 0;
}