C++;嵌套环形状 我一直在使用嵌套的循环来创建自己的C++程序,以创建某种形状。我最近的项目是创建一个类似这样的形状 * ** *** **** ***** ***** **** *** ** *

C++;嵌套环形状 我一直在使用嵌套的循环来创建自己的C++程序,以创建某种形状。我最近的项目是创建一个类似这样的形状 * ** *** **** ***** ***** **** *** ** *,c++,for-loop,nested-loops,C++,For Loop,Nested Loops,但是我已经写了一个程序,它给了我一个结果 * ** *** **** ***** ***** **** *** ** * 这是我的代码 #include <iostream> using namespace std; void main(){ //displays a top triangle going from 1 - 5(*) for (int i = 0; i <= 5; i++){ for (int j = 0; j <= i; j++){

但是我已经写了一个程序,它给了我一个结果

*
**
***
****
*****
*****
****
***
**
*
这是我的代码

#include <iostream>
using namespace std;

void main(){
//displays a top triangle going from 1 - 5(*)
for (int i = 0; i <= 5; i++){
    for (int j = 0; j <= i; j++){
        cout << "*";

    }

    cout << endl;
}
//displays a bottom to top triangle 5 - 1(*)
for (int k = 0; k <= 5; k++){
    for (int l = 5; l >= k; l--){
        cout  << "*";
    }

    cout << endl;
}
system("pause");
}
#包括
使用名称空间std;
void main(){
//显示从1到5(*)的顶部三角形

对于第二个嵌套循环中的(inti=0;i),不打印空格

使用一个三个空格的字符串,然后在每次运行内部循环后,将另一个空格附加到该字符串并打印它:

spc = "   ";
for (int k = 0; k <= 5; k++){
    cout << spc;
    for (int l = 5; l >= k; l--){
        cout  << "*";
    }
    spc += " ";
    cout << endl;
}
spc=”“;

对于第二个循环中的(int k=0;k),您需要:

std::string spc = "   "; // add #include <string> at the top of the file
for (int k = 0; k <= 5; k++) {
    cout << spc;
    for (int l = 5; l >= k; l--){
        cout << "*";
    }
    spc += " ";
    cout << endl;
}
std::string spc=“”;//在文件顶部添加#include
对于(int k=0;k您可以尝试以下方法:

希望这有帮助(需要优化)


我建议:在调试器中运行您的程序,并观察每次循环中发生的情况,直到您注意到发生了错误的输出和/或使用“铅笔和纸”运行您的程序,再次注意到输出开始出错时,注意循环在做什么,以及它出错的原因。我认为有趣的是,你建议那些不知道如何使这个程序工作的人只需加载
gdb
,然后进行黑客攻击。另外,你能告诉我你添加的代码对这个程序有什么作用吗这是我的错误。伙计,这是非常自我记录的。你还需要在
spc
之前输入一个类型,可能
std::string
@Andrey这对OP来说并不难,我没有编写完整的解决方案。你还需要
main
)@user3126681您的错误是没有打印任何空格,调试代码以更好地理解流程。
#include <iostream>

using namespace std;

int main()
{
    int i, j, k;
    for (i=0; i<5; i++){
        for (j=0; j<i+1; j++){
            cout << "*";
        }
        cout << endl;
    }
    for (i=0; i<5; i++){
        for (j=0; j<i+1; j++){
            cout << " ";
        }
        for (k=5; k>i+1; k--){
            cout << "*";
        }
        cout << endl;
    }
    return 0;
}
*
**
***
****
*****
 ****
  ***
   **
    *
 void printChars(int astrks, int spcs, bool bAstrkFirst)
 {
    if(bAstrkFirst)
    {
        for(int j = 0; j<astrks;j++)
        {
            std::cout<<"*";
        }
        for(int k = 0; k<spcs;k++)
        {
            std::cout<<" ";
        }
    }
    else
    {

        for(int k = 0; k<spcs;k++)
        {
            std::cout<<" ";
        }
        for(int j = 0; j<astrks;j++)
        {
            std::cout<<"*";
        }
    }
    std::cout<<std::endl;
}

int main()
{
    int astrk = 1, spc = 7;
    for(int x = 0; x<5; x++)
    {
        printChars(astrk++, spc--, true);
    }
    for(int x = 0; x<5; x++)
    {
        printChars(--astrk, ++spc, false);
    }
    getchar();
    return 0;
}
*
**
***
****
*****
   *****
    ****
     ***
      **
       *