/*猜數字游戲*/
#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