我正在尝试使用 for 循环制作一个简单的边框。我没有得到正确的结果。具体来说,我的右边框没有显示。请帮忙。
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 << "*";
}
最佳答案
在第二个循环中,您绘制了边框,但忘记绘制矩形的内部。
将此添加到if
:
else {
cout << " ";
}
正如 πìντα ῥεῖ 所指出的,您还忘记了在第一个和最后一个循环之后使用 endl
。
关于c++ - 使用 for 循环绘制边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37401451/