簡體   English   中英

需要幫助,使用for循環在C ++中跟蹤恆定寬度

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

我需要使用for循環進行這樣的操作,而不是單獨打印每行:

在此處輸入圖片說明

到目前為止,我有一個像直角三角形的東西,這是一個開始。

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;
}

}

該輸出看起來像: 在此處輸入圖片說明

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

希望這可以幫助。 您可以簡化很多操作,但是由於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