简体   繁体   English

需要帮助,使用for循环在C ++中跟踪恒定宽度

[英]need help making a track of constant width in C++ using for loop

I need to make something like this using a for loop instead of just printing each line seperatly: 我需要使用for循环进行这样的操作,而不是单独打印每行:

在此处输入图片说明

So far i have something like a right triangle which is a start. 到目前为止,我有一个像直角三角形的东西,这是一个开始。

void roadBound() {
const int ROW = 17;
const int GAP = 10;
const int NUM = -17;

for (int i=ROW, g=GAP, n = NUM ; i>=0; i--, g+=2)
{
    for (int j=n; j<i; j++) 
        cout << '*';
    for (int j=0; j<g; j++) 
        cout << ' ';
    for (int j=n; j<i; j++) 
        cout << '*';
    cout << endl;
}

} }

The output of that looks like : 该输出看起来像: 在此处输入图片说明

不要增加gap ,而要保持不变。

Hope this helps. 希望这可以帮助。 You can simplify it a lot but since OP is a newbie he will maybe understand it better like this. 您可以简化很多操作,但是由于OP是新手,因此他可能会对此有所了解。

int iteration = 0;
const int rowMaxSize = 25;
const int gapSize = 10;
const int leftSizeMin = 3;
const int leftSizeMax = 10;
const int iterationMax = 40;

int currentLeftSize = leftSizeMin;
bool increasing = true;

do {
    for(int i = 0; i < currentLeftSize; i++) {
        cout << "*"; 
    }
    for(int i = 0; i < gapSize; i++) {
        cout << " "; 
    }
    for(int i = 0; i < rowMaxSize - gapSize - currentLeftSize; i++) {
        cout << "*"; 
    }
    if(currentLeftSize >= leftSizeMax) {
        increasing = false;
    } else if (currentLeftSize == leftSizeMin) {
        increasing = true;
    }
    if(increasing) {
        currentLeftSize++;
    } else {
        currentLeftSize--;
    }
    cout << endl;
} while( iteration++ < iterationMax );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM