C++ 如何使用用户输入创建空心框/矩形
C++ How to create a hollow box/rectangle with user input?
我目前正在处理一个项目,其中提示用户输入他们想要用于其框的高度、宽度和字符。我必须使用 for 循环创建一个实心空心盒子。我已经创建了没有问题的固体,但是当涉及到空心的时,我遇到了一些问题。任何帮助,不胜感激。
int main()
{
int height;
int width;
int i, j;
char ch;
cout << "Please enter your height: ";
cin >> height;
cout << "Please enter your width: ";
cin >> width;
cout << "Please enter your character: ";
cin >> ch;
for (i = 1; i <= height; i++)
{
for (j = 1; j <= width; j++)
cout << ch;
cout << endl;
}
cout << "Press any key to continue to the next shape." << endl;
_getch();
for (i = 1; i <= height; i++)
{
for (j = 1; j <= width; j++)
{
if (i == 1 || i == width -1 || j == 1 || j == height )
cout << ch;
else cout << " ";
}
cout << endl;
}
system("pause");
return 0;
}
你可以把它写在嵌套的 for 循环中:
if( (i==1 || i==height) || (j==1 || j==width))
cout << ch;
else
cout << " ";
这是编写空心盒子的代码。 w
是宽度,h
是高度,c
是要使用的字符。
int w, h;
char c;
int i, j;
/* write the first line */
for (i = 0; i < w; ++i)
putchar(c);
putchar('n');
/* write the inner lines */
for (j = 0; j < h - 2; ++j) {
putchar(c);
for (i = 0; i < w - 2; ++i)
putchar(' ');
putchar(c);
putchar('n');
}
/* write the final line */
for (i = 0; i < w; ++i)
putchar(c);
putchar('n');
相关文章:
- 如何在QT中的自定义视频小工具t上绘制矩形
- 如何在Visual Basic中使用矩形函数OpenGL绘制矩形
- 矩形类中没有匹配函数
- 使用 stbi_write_png,如何将 0 和 1 的矩形字节数组转换为单色 png 文件?
- C++ SDL2:如何将矩形渲染到多个视口中
- 如何在 3D OpenGL 场景上绘制 SDL 2D 矩形?
- 如何在不使用静态矩形方法的情况下创建 IDOMPathGeometryPtr?
- SDL2 无法正确绘制矩形
- SFML白色矩形
- 解除分配矩形 2D 数组会根据尺寸产生错误
- 使用 SFML 在矩形的矢量中查找鼠标单击量
- 检查点是否位于 m 维矩形内
- 如何使用 opencv 绘制绿色矩形?
- 如何使用类获取矩形面积?
- SFML 在屏幕上移动矩形时滞后/小卡顿
- SDL/C++: 如何平滑但随机地移动矩形?
- 如何使用斐波那契和递归函数检查矩形是否是黄金矩形?
- 需要帮助使用类获取矩形和圆形的面积
- 打印两个反向空心矩形,然后在其内部打印两条对角线
- C++ 如何使用用户输入创建空心框/矩形