gpt4 book ai didi

c - 使用源代码绘制蛇的方形场

转载 作者:行者123 更新时间:2023-11-30 18:12:03 25 4
gpt4 key购买 nike

我正在尝试使用此代码创建一个方形字段。但不知怎么的,结果却是错误的。谁能帮我指出我的错误吗?我不确定我做错了什么,因为这是我第一次学习 C。这是代码

int i,j;

const int width = 30;

const int height = 30;

int main()
{
for(int i = 0; i < width + 2; i++){
printf("#");
}

for(int i = 0;i < height; i++){
for(int j = 0; j < width; j++){
if(j == 0){ printf("#") }
if(j == 0){ printf(" "); }
if(j == width - 1){ printf("#"); }
}
}

for(int i = 0; i < width + 2; i++){
printf("#");
}


return 0;
}

最佳答案

在 for 循环中,您想要更像

for (int i = 0; i < height; i++){
for (int j = 0; j < width + 2; j++){ //Note width + 2, to match your top line
if (j == 0){ printf("#"); } // print near border
else if (j == (width + 1)){ printf("#"); } // print far border
else { printf(" "); } // when it's not a far border or near border, print an empty space
}
printf("\n");
}

您不必为近边框打印“#”“”,只需打印“#”,然后为正方形内的所有空格打印“”。最后,对于最远的边缘,再次打印“#”。

关于c - 使用源代码绘制蛇的方形场,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41507176/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com