对于循环被跳过,为什么?
For loops are skipped, why?
我有一个任务,我必须在 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;
}
相关文章:
- 为什么 for 循环和 while 循环之间的 continue 不同?
- 为什么for循环中的异步不能提高执行时间
- 为什么无限循环会延迟打印?
- 为什么 for 循环只接受这个简单代码中的最后一个字符串?
- 为什么虚拟循环会消耗内核时间?
- 异常基础知识:为什么 while 循环会变成无限循环?
- 为什么这个循环每次迭代需要 1.32 个周期
- 为什么基于循环范围不直接在C 中使用参数阵列
- 为什么这个循环变量在循环范围之外是可访问的
- 为什么 for 循环不递增?
- 为什么 for 循环不能定义两个作用域的变量?
- 为什么这个循环模板实例化是合法的?
- 为什么 while 循环不需要用户在 C++ 中输入
- 为什么在循环外举起弦会导致性能较慢
- 为什么 For 循环中的 While 循环在我的程序中不起作用?
- C ++为什么在循环中创建对象时需要'new'
- 为什么在循环中重复一个内核会使CUDA代码明显较慢
- 在 c++ 中,c 为什么 for 循环块内部和 for 循环内的条件行为不同?
- 为什么在循环中定义的变量地址在每次迭代中都保持不变
- 为什么for循环中定义的变量只存在于循环内部