本文实例为大家分享了C语言实现猜数字游戏的具体代码,供大家参考,具体内容如下
实现效果如图:

代码如下:
- #include<stdio.h>
- #include<stdlib.h>
- #include<time.h>
- int main()
- {
- int Guess,Num; //Guess猜数,Num随机数
- int Next; //下一次执行
- do
- {
- system("cls"); //清屏
- printf("-------------------------------------\n");
-
- srand(time(NULL));
- Num=rand()%100+1; //随机生成1-100数字
-
- printf("请猜猜1-100内的数字:\n");
- scanf("%d",&Guess);
-
- while(Guess!=Num)
- {
- //scanf用于继续猜数判断
- if(Guess>Num)
- {
- printf("猜大啦.\n");
- scanf("%d",&Guess);
- }
- if(Guess<Num)
- {
- printf("猜小啦.\n");
- scanf("%d",&Guess);
- }
- }
-
- printf("恭喜你猜对啦!\n");
- printf("想不想再猜一猜?Y/y或N/n\n");
- getchar(); //输入是否继续执行字符y/n
- printf("-------------------------------------\n");
- Next=getchar(); //赋值
-
- }while(Next == 'Y' || Next == 'y'); //判断下次是否执行
-
- printf("程序已退出...\n");
- return 0;
- }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。