当前位置:编程学习 > C/C++ >>

c语言学习-猜数字游戏

/*猜数字游戏*/ 
#include "stdio.h"  
#include "stdlib.h" //产生随机数要用到的头文件  
main() 

 int x,count=5,number; 
 char answer; 
 system("graftable 936"); 
 clrscr(); 
 randomize(); 
 x=random(100)+1; /*产生1-100之间的随机数*/ 
 printf("\t\t\t\t[猜数字游戏]\n"); 
 printf("--------------------------------------------------------------------------------\n"); 
 printf("你有5次机会,请好好把握!\n"); 
 while(1) 
      { 
         printf("请输入1-100之间的数:"); 
         scanf("%d",&number); 
         count--; 
         if(number==x) 
           { 
              printf("恭喜你,你用%d次猜对了数字!\n",5-count); 
              printf("是否继续游戏?(y/n)"); 
              scanf("%s",&answer); 
              if(answer=='y') 
                { 
                   count=5; 
                   x=random(100)+1; 
                } 
              else 
                { 
                  printf("游戏结束,按任意键退出!"); 
                  break; 
                } 
           } 
         else 
           { 
               if(count>=1) 
                 { 
                     if(number>x) 
                       { 
                          printf("偏大啦!你还有%d次机会!\n",count); 
                       } 
                     else 
                       { 
                          printf("偏小啦!你还有%d次机会!\n",count); 
                       } 
                 } 
               else 
                 { 
                    printf("答错啦!正确答案为%d!\n",x); 
                    printf("是否继续游戏?(y/n)"); 
                    scanf("%s",&answer); 
                    if(answer=='y') 
                      { 
                        count=5; 
                        x=random(100)+1; 
                      } 
                    else 
                      { 
                        printf("游戏结束,按任意键退出!"); 
                        break; 
                      } 
                 } 
 
           } 
      } 
 getch(); 

 

 

摘自 wyzhangchengjin123

补充:软件开发 , C语言 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,