首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义drawRect()函数中是否存在错误或我的错误?

自定义drawRect()函数中是否存在错误或我的错误?
EN

Stack Overflow用户
提问于 2011-08-01 19:38:18
回答 2查看 80关注 0票数 0

我的drawRect()函数的行为就好像有一个bug。我正在尝试确定以下代码中是否存在bug,如果不是,则确定我的操作是否不正确。

我正在尝试制作一个在顶部屏幕上显示一个矩形的函数。如果程序员键入"drawRect(3,3)",就会创建一个3 x 3的矩形。但是,如果程序员键入"drawRect(3,4)",则会显示矩形的右上角,然后显示一个无限长的顶部。有人能帮帮我吗?下面是我的代码:

代码语言:javascript
复制
#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);
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-08-01 19:44:34

在您的循环中,您应该递增widthloopheightloop而不是widthheight。另外,widthloopheightloop应该初始化为0。

票数 2
EN

Stack Overflow用户

发布于 2011-08-01 19:44:04

像这样的循环:

代码语言:javascript
复制
for(widthloop=1;widthloop<width-2;width++)

和:

代码语言:javascript
复制
for(heightloop=1;heightloop<height-2;height++)

width <= 3height <= 3时将不执行任何操作。

还要注意,widthloopheightloop实际上应该是局部变量,尽管这本身不是bug,只是“代码气味”。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6897864

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档