You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C89动态尺寸带边框二维数组绘制错误求助

修复建议

问题根源

代码里边框判断逻辑完全混淆了height和width的对应关系,同时索引边界处理错误:

  • 行索引i的有效范围是0到height-1,底部边框应该判断i == height-1,而非i == width
  • 列索引j的有效范围是0到width-1,右边框应该判断j == width-1,而非j == height
  • 函数参数缺少类型声明,不符合标准C语法规范

修复后的代码

#include<stdio.h>
#include<stdlib.h>

/* variables */
int j, i, gameover, score;
int x, y, fruitx, fruity, flag;

/* Function to draw a boundary */
void draw(int height, int width)  // 补充参数类型声明
{
    /* system("cls"); */
    for (i = 0; i < height; i++) {
        for (j = 0; j < width; j++) {
            // 修正边框判断:行首/行尾、列首/列尾
            if (i == 0 || i == height - 1 || j == 0 || j == width - 1) {
                printf("#");
            }
            else {
                printf(" ");
            }
        }
        printf("\n");
    }
}

效果验证

当输入height=20、width=60时:

  • 顶部(i=0)和底部(i=19)的每一列都会打印#,形成完整的上下边框
  • 每一行的首列(j=0)和末列(j=59)都会打印#,形成完整的左右边框
  • 内部区域填充空格,符合预期的带边框二维数组效果

内容的提问来源于stack exchange,提问作者user20010604

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 11:45:40