[英]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.