首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用for循环绘制边框

使用for循环绘制边框
EN

Stack Overflow用户
提问于 2016-05-23 22:18:24
回答 1查看 1.8K关注 0票数 0

我试着用for循环做一个简单的边框。我没有得到适当的结果。具体来说,我的右边框没有显示。请帮帮忙。

代码语言:javascript
复制
const int width = 20;
const int height = 20;

void Drow() 
{
    system("cls");                      // clear the screan
    for (int i = 0; i < width; ++i) 
    {
        cout << "*";                    // upper border
    }       

    for (int i = 0; i < height-2; i++) 
    {
        for (int j = 0; j < width; j++) 
        {
            if (j == 0 || j == width - 1)
            {
                cout << "*";            // left and right borders
            }
        }
        cout << endl;
    }

    for (int i = 0; i < width; i++)     // lower border
        cout << "*";
}
EN

回答 1

Stack Overflow用户

发布于 2016-05-23 23:48:40

除非我真的觉得受虐狂,否则我会做一些不同的工作。

我的直接反应是编写更多关于这个一般顺序的代码:

代码语言:javascript
复制
std::string h_border = std::string(width, '*' ) + "\n";
std::string v_border = "*" + std::string(width - 2,  ' ') +"*\n";

std::cout << h_border;
for (int i = 0; i < height - 2; i++)
    std::cout << v_border;
std::cout << h_border;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37401451

复制
相关文章

相似问题

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