int i, j, k;int num = 0;/*生成随机数字*/num = rand();printf("%d\n", num);
选择玩游戏还是退出
void play(int x)
{printf("%d\n", x);printf("开始游戏");
}
scanf("%d", &k);switch (k){case 1:play(num);case 2:break;}
循环玩游戏程序
void play(int x)
{printf("%d\n", x);printf("开始游戏");printf("游戏结束");
}
int main()
{int i, j, k;int num = 0;/*生成随机数字*/num = rand();printf("%d\n", num);again:{scanf("%d", &k);switch (k){case 1:play(num);goto again;case 2:break;}}
书写玩游戏内部程序
void play(int x)
{int number=0;printf("请输入数字\n");again_1:{scanf("%d", &number);while (number != x){if (number > x){printf("Big"); goto again_1;}else{printf("Small"); goto again_1;}}printf("Right\n请选择\n\n");}
}
到这里游戏的整体框架编译完成,可以正常运行。但是我们发现每一次的数字都是41(或者其他固定数字)
/*
***猜数字游戏
*/void play(int x)
{int number=0;printf("请输入数字\n");again_1:{scanf("%d", &number);while (number != x){if (number > x){printf("Big"); goto again_1;}else{printf("Small"); goto again_1;}}printf("Right\n请选择\n\n");}
}int main()
{int i, j, k;int num = 0;/*生成随机数字*/num = rand();again:{printf("开始游戏\n1.开始\n2.退出\n");scanf("%d", &k);switch (k){case 1:play(num);goto again;case 2:break;}}system("pause");return 0;
}
关于<time.h>头文件
生成随机数字
/*生成随机数字*///num = rand();srand(time(NULL));num = rand() % 100
最后完成的程序
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/*
***猜数字游戏
*/void play(int x)
{int number=0;printf("请输入数字\n");again_1:{scanf("%d", &number);while (number != x){if (number > x){printf("Big\n"); goto again_1;}else{printf("Small\n"); goto again_1;}}printf("\n***Right***\n *********\n ******\n ****\n **\n");}
}int main()
{int k;int num = 0;/*生成随机数字*///num = rand();srand(time(NULL));num = rand() % 100;again:{printf("开始游戏\n1.开始\n2.退出\n");scanf("%d", &k);switch (k){case 1:play(num);goto again;case 2:break;}}system("pause");return 0;
}
详细介绍的关于随机生成数字的解析
https://mp.csdn.net/postedit/79788815