我的drawRect()函数表现得好像有错误一样。我试图确定以下是否存在错误,或者,如果不是我正在做错误的错误。
我正在尝试制作一个在顶部屏幕上显示矩形的功能。如果程序员输入“drawRect(3,3)”,则会创建一个3乘3的矩形。然而,如果程序员键入“drawRect(3,4)”,则显示矩形的右上角,然后显示无限长的顶部。有人能帮助我吗?这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#define SIDES 0xB3
#define TOP_RIGHT 0xBF
#define BOTTOM_LEFT 0xC0
#define TOP_BOTTOM 0xC4
#define BOTTOM_RIGHT 0xD9
#define TOP_LEFT 0xDA
int heightloop;
int widthloop;
int displayrect(int height, int width)
{
printf("%c",TOP_LEFT);
for(widthloop=1;widthloop<width-2;width++)
{
printf("%c",TOP_BOTTOM);
}
printf("%c\n",TOP_RIGHT);
for(heightloop=1;heightloop<height-2;height++)
{
printf("%c",SIDES);
for(widthloop=1;widthloop<width-2;width++)
{
printf(" ");
}
printf("%c\n",SIDES);
}
printf("%c",BOTTOM_LEFT);
for(widthloop=1;widthloop<width-2;width++)
{
printf("%c",TOP_BOTTOM);
}
printf("%c",BOTTOM_RIGHT);
return(0);
}
答案 0 :(得分:2)
在你的循环中,你应该增加widthloop
和heightloop
而不是width
和height
。 widthloop
和heightloop
也应该用0初始化。
答案 1 :(得分:0)
这样的循环:
for(widthloop=1;widthloop<width-2;width++)
和
for(heightloop=1;heightloop<height-2;height++)
在width <= 3
或height <= 3
时无效。
另请注意,widthloop
和heightloop
应该是本地变量,虽然这不是本身的错误,只是“代码味道”。