对于循环重复绘制问题
For loop repetition drawing trouble
我试图让一个循环来画一棵圣诞树,但输出是错误的,我尝试寻找答案,但我似乎找不到它,我被难住了。答案可能很明显,但我错过了很多,非常感谢任何帮助!
#include <iostream>
#include <assert.h>
#include <iomanip>
using namespace std;
const char blank = ' ';
const char leaf = '#';
const char wood = '|';
const int minSize = 4;
const int maxSize = 20;
int treeHeight;
int& getValidHeight(int&);
void drawALineOfFoliage(int);
void drawFoliage(int);
void drawTrunk(int);
void drawAXmasTree(int);
void drawAXmasTree(int treeHeight) {
getValidHeight(treeHeight);
drawFoliage(treeHeight);
drawTrunk(treeHeight);
}
int& getValidHeight(int& treeHeight) {
cout << ("Please enter the size of the tree (4-20):n");
cin >> treeHeight;
while ((treeHeight < minSize) || (maxSize < treeHeight)) {
cout << "ERROR: Invalid height! Enter the size of the tree (4-20):n";
cin >> treeHeight;
return treeHeight;
}
}
void drawALineOfFoliage(int treeHeight) {
for (int x = 0; x < treeHeight; ++x){
for (int y = treeHeight; y > x; --y){
cout << blank;}
for (int y = 0; y < x; ++y){
cout << leaf;}}}
void drawFoliage(int treeHeight) {
int branchLine = 1;
do {
drawALineOfFoliage(treeHeight);
branchLine += 1;
} while (branchLine <= (treeHeight - 2));}
void drawTrunk(int treeHeight) {
int trunkLine(1), spaces;
while (trunkLine <= 2) {
spaces = 1;
while (spaces <= (treeHeight - 3)) {
cout << blank;
spaces += 1;}
cout << wood << "n";
trunkLine += 1;
}
}
int main()
{
drawAXmasTree(treeHeight);
system("pause");}
输出只是解构的圣诞树,所以所有 关卡在同一条线上并重复多次
所以我拿了你的示例代码并试了一下。
首先,您的样式和制表符不一致,这会使代码非常难以阅读。
接下来,你的drawALineOfFoliage
实际上是绘制没有树干的整棵树,而不仅仅是一条线。因此,在其他 2 个嵌套的 for 循环之后,您在主 for 循环中缺少一个cout << endl;
。
开始编辑:
编辑:我忘了谈论半棵树。
因此,在现有代码中,它只打印树的一半。像这样的东西...
#
##
###
####
这是半棵圣诞树。为了使它看起来像一棵真正的树,我所做的只是将*2
添加到负责打印叶子的 for 循环中。(您也可以改为cout << leaf << leaf;
)
for (int y = 0; y < x*2; ++y) {
cout << leaf;
}
结束编辑。
由于您的drawALineOfFoliage
已经在打印树,因此在drawFoliage
中,
do {
drawALineOfFoliage(treeHeight);
branchLine += 1;
} while (branchLine <= (treeHeight - 2));
这个 do while 循环正在循环树木的数量(同样,没有树干),所以应该删除它。
现在我们已经完成了顶部,让我们来看看后备箱。
while (spaces <= (treeHeight - 3))
- 3
似乎凭空而来。而且它每行只打印一个|
,看起来有点奇怪,所以我删除了- 3
,让它打印 2 块木头。
现在输出看起来有点像这样...
Please enter the size of the tree (4-20):
6
##
####
######
########
##########
||
||
没事,但仍然很奇怪。TLDR,我做了一些调整,得到了这样的最终结果......
Please enter the size of the tree (4-20):
7
#
###
#####
#######
#########
###########
#############
|||
|||
完整代码如下:
#include <iostream>
#include <assert.h>
#include <iomanip>
using namespace std;
const char blank = ' ';
const char leaf = '#';
const char wood = '|';
const int minSize = 4;
const int maxSize = 20;
int treeHeight;
int& getValidHeight(int&);
void drawALineOfFoliage(int);
void drawFoliage(int);
void drawTrunk(int);
void drawAXmasTree(int);
void drawAXmasTree(int treeHeight) {
getValidHeight(treeHeight);
drawFoliage(treeHeight);
drawTrunk(treeHeight);
}
int& getValidHeight(int& treeHeight) {
cout << ("Please enter the size of the tree (4-20):n");
cin >> treeHeight;
while ((treeHeight < minSize) || (maxSize < treeHeight)) {
cout << "ERROR: Invalid height! Enter the size of the tree (4-20):n";
cin >> treeHeight;
}
return treeHeight;
}
void drawALineOfFoliage(int treeHeight) {
for (int x = 0; x < treeHeight; ++x) {
for (int y = treeHeight; y > x; --y) {
cout << blank;
}
for (int y = 0; y < x*2; ++y) {
cout << leaf;
}
cout << endl;
}
}
void drawALineOfFoliageOdd(int treeHeight) {
for (int x = 0; x < treeHeight; ++x) {
for (int y = treeHeight; y > x; --y) {
cout << blank;
}
cout << leaf;
for (int y = 0; y < x*2; ++y) {
cout << leaf;
}
cout << endl;
}
}
void drawFoliage(int treeHeight) {
int branchLine = 1;
drawALineOfFoliageOdd(treeHeight);
do {
branchLine += 1;
} while (branchLine <= (treeHeight - 2));
}
void drawTrunk(int treeHeight) {
int trunkLine(1), spaces;
while (trunkLine <= 2) {
spaces = 1;
while (spaces <= (treeHeight - 1)) {
cout << blank;
spaces += 1;
}
cout << wood << wood << wood << endl;
trunkLine += 1;
}
}
int main() {
drawAXmasTree(treeHeight);
system("pause");
}
注意:我没有进行任何形式的清理,因此其中有很多不必要的代码。我只是在你必须为你提供我得到的最佳解决方案的基础上工作。
您的lineOfFoliage
似乎为所有x < height
做行。 在我看来,树叶绘制代码的基本结构是:
drawFoliage(height) {
for(width = 0..height) cout << centeredLine(width);
}
drawCenteredLine(width) {
return blanks + leafs;
}
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- 绘制文本问题
- 我正在尝试创建一个 C++ 贪吃蛇游戏,但我似乎无法绘制第二个高度边框,我该如何解决这个问题?
- 在 Qt C++ 中在自定义项委托上绘制文本时的性能问题
- 绘制的 OpenGL 点消失,绘制调用和交换缓冲区问题
- 绘制非常大的 2D 图形时出现问题
- 对于循环重复绘制问题
- 绘制多边形网格时出现 Opengl 性能问题
- TMX问题,在正确位置绘制瓷砖(c++)
- 使用纹理,法线和索引列表从VBO绘制OpenGL对象的问题
- 简单的地图绘制问题
- 使用SpriteFont类绘制文本时出现问题
- 在 SDL 中,当我在屏幕上绘制菜单时,它会使游戏变慢并且无法正常工作,我该如何解决这个问题?
- 为什么我的非所有者绘制的列表框在添加字符串后会出现绘图问题
- opengl =中缓冲区数据的问题仅在我缓冲比需要更多的字节时绘制
- 在OpenGL中绘制线的问题
- Direct3d绘制2d精灵.扩展问题
- Q项目绘制问题