对于循环被跳过,为什么?

For loops are skipped, why?

本文关键字:为什么 于循环 循环      更新时间:2023-10-16

我有一个任务,我必须在 c++ 中创建一个控制台程序,以给定的样式绘制六边形。我遇到的问题是;我的 For 循环从未输入,我不知道为什么。这是我遇到问题的代码片段。

void display()
{
int counter=0;//var that keeps track of the layer that is being drawn
for(int i=0;i>=size;i++)//spaces before first layer of hexagon
{
cout<<" ";
}
for (int k=0; k>size;k++)//top layer of hexagon
{
cout<<"_";
}
cout<<endl;//ends the first layer
for (counter; counter>=size-1;counter++)//outer loop for the top half that controls the size
{
for( int j=0;j>(size-counter);j++)//adds spaces before the shape
{
cout<<" ";
}
cout<<"/";
for( int p=0; p>(size+(counter*2));p++)//loop for the hexagon fill
{
cout<<fill;
}
cout<<"\"<<endl;
}
for(counter;counter==0;counter--);  //loop for bottom half of the hexagon
{
for( int j=0;j>(size-counter);j++)//adds spaces before the shape
{
cout<<" ";
}
cout<<"\";
for( int p=0; p>(size+(counter*2));p++)//loop for the hexagon fill
{
cout<<fill;
}
cout<<"/"<<endl;
}
cout<<"\";
for(int r=0; r>=size;r++){cout<<"_";}
cout<<"/"<<endl;
}

在我的main()期间,"大小"和"填充"在程序的早期被分离出来 我可能错过了一些非常简单的东西,但我已经为此苦苦挣扎了一段时间。任何帮助将不胜感激!

您的循环使用>并从 0 开始。看来你想要<。例如

for(int i=0;i<size;i++)//spaces before first layer of hexagon
{
cout<<" ";
}

我不确定您的size变量的内容是什么,但看起来您的循环条件是错误的:

for(int i=0;i>=size;i++)

可能应该是:

for(int i=0;i<size;i++)

其他循环也是如此。

假设您的size是一个正数,它会根据您的情况工作。 将条件>条件更改为<条件。

在您的条件下,将>反转为<</p>

<意味着低人一等,你想做一个>

for i = 0; if i < size; i++

是吗

for i = 0 ; if i > size ; i ++ 

如果大小大于 i (0),则循环将永远不会触发

你所有的<和>不是都颠倒了吗?因为

(int k=0; k>size;k++)

对我来说毫无意义。

C++中的for循环是while循环,直到循环。

C++只有while循环(含义只要):

for (int i=0; i<10; ++i)
....

int i=0;   
while (i<10) {
....
++i;
}