1、猜数游戏
涉及知识点:循环、随机数操作。
计算机“想”一个数([1,100])请人猜,如果猜对了,提示正确信息;否则告诉所猜的数是大还是小,同时提示下一次猜数的范围,直到猜对结束,最后给出游戏者猜测的次数。要求,程序以菜单方式工作。
附件1 随机数的生成:
下面示例代码生成10个1~100范围内的随机整数,存入数组array中。
#include <stdlib.h>
void test()
{
int array[10],i;
srand(time(0)); //设置随机数种子,必须要有
for(i=0;i<10;i++)
{
array[i]=rand()%100+1; //生成1~100范围内的随机整数,赋值给数组
//元素array,生成a-b.之间的一个随机整数公式为:rand0%(b-a+1)+a;
}
}
猜数游戏,请大家集思广益,需要给出测试次数,需要用到下面的附件
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
7条回答 默认 最新
技术专家团-小桥流水 2022-02-12 10:30关注菜单方式猜数游戏代码及运行结果如下:
菜单:
游戏过程及提示(提示数据范围):

#include <stdio.h> #include <stdlib.h> #include <time.h> void caishu(int n) { int c,count=0; int max=100,min=1; system("cls"); while(1) { printf("请输入一个数:"); scanf("%d",&c); count++; if(c>n) { max = c; printf("太大了,数应该在%d-%d之间。",min,max); } else if(c<n) { min = c; printf("太小了,数应该在%d-%d之间。",min,max); } else { printf("猜对了,共猜了%d次\n",count); system("pause"); return; } } } int main() { int n; int op; srand((unsigned int)time(0)); //菜单 while(1) { system("cls"); printf("欢迎使用猜数游戏\n"); printf("1.开始游戏\n"); printf("2.退出游戏\n"); scanf("%d",&op); switch(op) { case 1: n = rand()%100+1; caishu(n); break; case 2: return 0; } } }本回答被题主选为最佳回答 , 对您是否有帮助呢?评论 打赏 举报解决 1无用