#include<graphics.h>
#include<stdio.h>
#define LEFT 120
#define TOP 40
#define SMALL 40
#define RADIUS 20
void draw(int i,int j);
void block(int i,int j);
void map(void);
void drac(int i,int j);
main()
{int i,j;
int gdriver=DETECT,gmode,errorcode;
char ch;
initgraph(&gdriver,&gmode,"F:\\TC");
errorcode=graphresult();
if(errorcode!=grOk)
{ printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
map();
getch();
closegraph();
return 0;
}
/*draw a cake*/
void draw(int i,int j)
{setfillstyle(SOLID_FILL,RED);
rectangle(LEFT+i*SMALL,TOP+j*SMALL,LEFT+(i+1)*SMALL,TOP+(j+1)*SMALL);
}
/*draw s cricle*/
void drac(int i,int j)
{setfillstyle(SOLID_FILL,BLUE);
circle(LEFT+i*SMALL+RADIUS,TOP+j*SMALL+RADIUS,RADIUS);
}
/*draw a block*/
void block(int i,int j)
{setfillstyle(SOLID_FILL,BLACK);
bar(LEFT+i*SMALL,TOP+j*SMALL,LEFT+(i+1)*SMALL,TOP+(j+1)*SMALL);
}
/*map*/
void map(void)
{int i,j;
int a[10][10]={{1,1,1,1,1,1,1,1,1,1},
{1,0,0,3,0,0,0,0,0,1},
{1,0,3,0,0,0,0,0,0,1},
{1,0,0,0,2,0,0,0,0,1},
{1,0,3,0,2,3,0,0,0,1},
{1,0,0,0,2,3,0,0,0,1},
{1,0,3,0,0,0,0,0,0,1},
{1,0,3,0,0,0,0,3,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}};
for(i=0;i<=9;i++)
{for(j=0;i<=9;j++)
{ if(a[i][j]==1) block(i,j);
else if(a[i][j]==2) drac(i,j);
else if(a[i][j]==3) draw(i,j);
}
}
}运行时就白屏啊,完全和预期的不一样啊!!
參考答案:for(i=0;i<=9;i++)
{for(j=0;i<=9;j++) (i改为j)
{ if(a[i][j]==1) block(i,j);
else if(a[i][j]==2) drac(i,j);
else if(a[i][j]==3) draw(i,j);
}
要细心啊!!