#include
#include
#include
#include
#define times 10
int number()
{
int i,number,t;
int a[3];
srand(time( NULL ));
for(i=0;i<3;i++)
{
t=rand()%10;
a[i]=t;
}
if(a[0]==0)
a[0]=a[0]+1;
number=a[0]*100+a[1]*10+a[2];
printf("\n\n%d\n",number);
return(number);
}
game()
{
int n;
printf("请选择:");
printf("1 继续游戏");
printf("0 退出游戏");
do
{
scanf("%d",&n);
if(n==1)
{
printf("请继续!");
main();
}
else
if(n==0)
{
printf("\n退出游戏!\n欢迎您下次使用!\n再见!\n");
exit(0);
}
else printf("输入错误!\n");
}while(n!=1||n!=0);
}
main()
{
int num,answer;
int i,j=0,t=100;
int a[3];
int b[3];
int *p1,*p2;
p1=b;
p2=a;
num=number();
while(j<10)
{
printf("请输入你猜的数(100-999):");
scanf("%d",&answer);
if(num>answer||num<answer)
{
for(i=0;i<3;i++,p1++,p2++)
{
a[i]=answer/t%10;
b[i]=num/t%10;
t=t/10;
if(*p1==*p2)
printf("第%d位数正确!\n",i);
else
if(*p1>*p2)
printf("第%d位数小了!\n",i);
else
printf("第%d位数大了!\n",i);
}
j++;
}
if(num==answer)
{
printf("恭喜你,猜中了!!!\n");
printf("所猜数字为:%d\n\n",num);
game();
}
}
}
你这太乱了。。。出什么错误了。 执行的结果不对吗
你好 两个错误
一、首先定义方法要有返回值类型,game()方法和mian()方法均为指定返回值类型,修改为 void game(){ } 或者 void main(){ }
二、C++入口函数为main()函数,按照运行顺序应该由main()函数去调用game() 函数,如果报错找不到调用的函数,则需要在方法前面预定义调用函数。
改成如下:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#define times 10
int number()
{
int i,number,t;
int a[3];
srand(time( NULL ));
for(i=0;i<3;i++)
{
t=rand()%10;
a[i]=t;
}
if(a[0]==0)
a[0]=a[0]+1;
number=a[0]*100+a[1]*10+a[2];
printf("\n\n%d\n",number);
return(number);
}
void main2();
void game()
{
int n;
printf("请选择:");
printf("1 继续游戏");
printf("0 退出游戏");
do
{
scanf("%d",&n);
if(n==1)
{
printf("请继续!");
main2();
}
else
if(n==0)
{
printf("\n退出游戏!\n欢迎您下次使用!\n再见!\n");
exit(0);
}
else printf("输入错误!\n");
}while(n!=1||n!=0);
}
void main2()
{
int num,answer;
int i,j=0,t=100;
int a[3];
int b[3];
int *p1,*p2;
p1=b;
p2=a;
num=number();
while(j<10)
{
printf("请输入你猜的数(100-999):");
scanf("%d",&answer);
if(num>answer||num<answer)
{
for(i=0;i<3;i++,p1++,p2++)
{
a[i]=answer/t%10;
b[i]=num/t%10;
t=t/10;
if(*p1==*p2)
printf("第%d位数正确!\n",i);
else
if(*p1>*p2)
printf("第%d位数小了!\n",i);
else
printf("第%d位数大了!\n",i);
}
j++;
}
if(num==answer)
{
printf("恭喜你,猜中了!!!\n");
printf("所猜数字为:%d\n\n",num);
game();
}
}
}
void main()
{
main2();
}