C语言编写猜数字游戏
编写一个猜数的小游戏,在程序里设定一个数,运行程序让别人猜.如果猜的数比设定的数大,给出"大了"的提示,如果猜的数比设定的数小,则给出"小了"的提示,设定有5次机会,5次...
编写一个猜数的小游戏,在程序里设定一个数,运行程序让别人猜.如果猜的数比设定的数大,给出"大了"的提示,如果猜的数比设定的数小,则给出"小了"的提示,设定有5次机会,5次内猜中给出"猜中"的提示,否则
否则提示“你输了” 展开
否则提示“你输了” 展开
2个回答
展开全部
#include <stdlib.h>
#include <stdio.h>
//#include <iostream>
unsigned char Num[4] = {0},getNum[4] = {0x20,0x20,0x20,0x20};
void help(void)
{
char i =0,j = 0;
for(i =0;i<4;i++)
{
for(j = 0;j<4;j++)
if(Num[j] == getNum[i])
{
if(i==j)
printf("%dth is correct!\r\n",i);
else
{
printf("%d is a correct data\r\n",getNum[i]);
}
}
else
{
if(i == 3&&j==3)
{printf("/********************small game*****************/\
input 4 data gess the random data,input h can get help\
");}
}
}
}
void main( void )
{
int orginal = 0,getdata = 0,i = 0,j = 0;
// { int a;a = 878*101;cout<<a<<endl}
//first = 0,second = 0,third = 0,fourth = 0;
/* Seed the random-number generator with GetTickCount so that
the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );
orginal = rand();
Num[3] = orginal%10000/1000;
Num[2] = orginal%1000/100;
Num[1] = orginal%1000%100/10;
Num[0] = orginal%10;
// printf(" %d",orginal);
for(i = 0;i<4;)
{
for(j = i+1;j<4;j++)
{
if(Num[i]==Num[j])
{ Num[i]++;if(Num[i]>9) Num[i] = 0;i=0;break;}
}
if(j == 4)i++;
}
get: for(i = 0;i<4;)
{
printf("input Num %d ",i);
scanf("%s",&getNum[i]);
if(getNum[i]-0x30>9||getNum[i]-0x30<0)
{
if (getNum[i] == 'H'||getNum[i] =='h')
{
help();
}
else
printf("input one num\r\n");
//if(i>0)i--;
}
else
{
getNum[i] -=0x30;
for(j = 0;j<i;j++)
{
if(getNum[i] == getNum[j])
{printf("have a same data,please input another\r\n"); break;}
}
if(j==i)
i++;
}
}
printf("you input num %d%d%d%d Y or N?\r\n",getNum[0],getNum[1],getNum[2],getNum[3]);
do{
scanf("%c",&j);
printf("%c",j);
if((j == 'N') || (j == 'n'))goto get;
}while( !((j == 'Y') || (j == 'y')));
printf("over");
}
#include <stdio.h>
//#include <iostream>
unsigned char Num[4] = {0},getNum[4] = {0x20,0x20,0x20,0x20};
void help(void)
{
char i =0,j = 0;
for(i =0;i<4;i++)
{
for(j = 0;j<4;j++)
if(Num[j] == getNum[i])
{
if(i==j)
printf("%dth is correct!\r\n",i);
else
{
printf("%d is a correct data\r\n",getNum[i]);
}
}
else
{
if(i == 3&&j==3)
{printf("/********************small game*****************/\
input 4 data gess the random data,input h can get help\
");}
}
}
}
void main( void )
{
int orginal = 0,getdata = 0,i = 0,j = 0;
// { int a;a = 878*101;cout<<a<<endl}
//first = 0,second = 0,third = 0,fourth = 0;
/* Seed the random-number generator with GetTickCount so that
the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );
orginal = rand();
Num[3] = orginal%10000/1000;
Num[2] = orginal%1000/100;
Num[1] = orginal%1000%100/10;
Num[0] = orginal%10;
// printf(" %d",orginal);
for(i = 0;i<4;)
{
for(j = i+1;j<4;j++)
{
if(Num[i]==Num[j])
{ Num[i]++;if(Num[i]>9) Num[i] = 0;i=0;break;}
}
if(j == 4)i++;
}
get: for(i = 0;i<4;)
{
printf("input Num %d ",i);
scanf("%s",&getNum[i]);
if(getNum[i]-0x30>9||getNum[i]-0x30<0)
{
if (getNum[i] == 'H'||getNum[i] =='h')
{
help();
}
else
printf("input one num\r\n");
//if(i>0)i--;
}
else
{
getNum[i] -=0x30;
for(j = 0;j<i;j++)
{
if(getNum[i] == getNum[j])
{printf("have a same data,please input another\r\n"); break;}
}
if(j==i)
i++;
}
}
printf("you input num %d%d%d%d Y or N?\r\n",getNum[0],getNum[1],getNum[2],getNum[3]);
do{
scanf("%c",&j);
printf("%c",j);
if((j == 'N') || (j == 'n'))goto get;
}while( !((j == 'Y') || (j == 'y')));
printf("over");
}
追问
请不要复制黏贴好吗?
追答
好的 不好意思
展开全部
#include<stdio.h>
#include<time.h>
#include<math.h>
int main()
{
int i,a,n;
srand((int)time(NULL));
n=rand()%100+1;
for(i=0; i<5; i++)
{
printf("请输入一个介于1到100的整数:");
scanf("%d",&a);
if(a<n)
printf("猜小了!\n");
if(a>n)
printf("猜大了!\n");
if(a==n)
{
printf("猜对了!\n");
break;
}
}
return 0;
}
#include<time.h>
#include<math.h>
int main()
{
int i,a,n;
srand((int)time(NULL));
n=rand()%100+1;
for(i=0; i<5; i++)
{
printf("请输入一个介于1到100的整数:");
scanf("%d",&a);
if(a<n)
printf("猜小了!\n");
if(a>n)
printf("猜大了!\n");
if(a==n)
{
printf("猜对了!\n");
break;
}
}
return 0;
}
追问
提示“你输了”这句没有。。
追答
忘记了,修改如下:
#include
#include
#include
int main()
{
int i,a,nflag=0;
srand((int)time(NULL));
n=rand()%100+1;
for(i=0; in)
printf("猜大了!\n");
if(a==n)
{
printf("猜对了!\n");
flag=1;
break;
}
}
if(!flag && i==5)
printf("你输了!");
return 0;
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询