C++ 计算嵌套for循环的值时遇到问题

C++ 计算嵌套for循环的值时遇到问题,c++,C++,我需要按以下方式打印星号: xxxxxxx _xxxxx __xxx (7、5、3) 或 xxxxxx _xxxx __xx (6、4、2) 有一个用于输入顶部星号数和行数的输入。最下面一行上的星号不得少于两个 我意识到这需要一个嵌套的for循环 for (rows = 1; rows < ((toprow / 2) + (toprow % 2)); rows++) { if(rows <= maxrows) { // first the le

我需要按以下方式打印星号:

xxxxxxx
_xxxxx
__xxx

(7、5、3)

xxxxxx
_xxxx
__xx

(6、4、2)

有一个用于输入顶部星号数和行数的输入。最下面一行上的星号不得少于两个

我意识到这需要一个嵌套的
for
循环

for (rows = 1; rows < ((toprow / 2) + (toprow % 2)); rows++)
  {
    if(rows <= maxrows)
      {
        // first the leading underscores
        for (int k=1; k<rows; ++k)
          cout << "_";

        // then the asterisks
        for (int k=1; k <= toprow - 2*rows + 2; ++k)
          cout << "*";
        cout << endl;                                    
      }
  }
我有以下代码:

  for (rows = 1; rows < ((toprow / 2) + (toprow % 2)); rows++)
    {
      for ()
      cout << endl;                                    
    }
for(行=1;行<((顶行/2)+(顶行%2));行++)
{
对于()
cout
for(行=1;行<((顶行/2)+(顶行%2));行++)
{
如果(行
for(行=1;行<((toprow/2)+(toprow%2));行++)
{
如果(行
for(行=1;行<((toprow/2)+(toprow%2));行++)
{
如果(行
for(行=1;行<((toprow/2)+(toprow%2));行++)
{

如果(行您可能想要这样的内容:

int initial = 7;
int copy = initial;
int rows = 3;
for (int i = 0; i < rows; i++)
{
    std::cout << std::string(initial - copy, ' ')
              << std::string(copy, '*') << std::endl;
    copy -= 2;
}
int initial=7;
int copy=初始值;
int行=3;
对于(int i=0;istd::cout您可能想要这样的东西:

int initial = 7;
int copy = initial;
int rows = 3;
for (int i = 0; i < rows; i++)
{
    std::cout << std::string(initial - copy, ' ')
              << std::string(copy, '*') << std::endl;
    copy -= 2;
}
int initial=7;
int copy=初始值;
int行=3;
对于(int i=0;istd::cout您可能想要这样的东西:

int initial = 7;
int copy = initial;
int rows = 3;
for (int i = 0; i < rows; i++)
{
    std::cout << std::string(initial - copy, ' ')
              << std::string(copy, '*') << std::endl;
    copy -= 2;
}
int initial=7;
int copy=初始值;
int行=3;
对于(int i=0;istd::cout您可能想要这样的东西:

int initial = 7;
int copy = initial;
int rows = 3;
for (int i = 0; i < rows; i++)
{
    std::cout << std::string(initial - copy, ' ')
              << std::string(copy, '*') << std::endl;
    copy -= 2;
}
int initial=7;
int copy=初始值;
int行=3;
对于(int i=0;istd::cout使用此C代码作为参考。它将打印到行数或最后一行开始数小于2时,以较早者为准

    #include<stdio.h>
    int main()
    {
    //topstar have the number of stars in top row
    int row=7;
    int topstar=7;
    int i,j,k,l,last;

    for(i=0;i<row;i++)
    {
    //last contain the number of stars in last row
    last=topstar - 2*i;

    if(last<2)
     break;

     for(k=0;k<i;k++)
       printf("_");
    for(j=0;j<last;j++)
       printf("*");
    for(l=0;l<i;l++)
       printf(" ");
       printf("\n");
   }
  return 0;
}
#包括
int main()
{
//topstar在第一行有多少颗星星
int行=7;
int-topstar=7;
int i,j,k,l,last;

对于(i=0;i使用此C代码作为参考。它将打印到行数或最后一行开始数小于2时,以较早者为准

    #include<stdio.h>
    int main()
    {
    //topstar have the number of stars in top row
    int row=7;
    int topstar=7;
    int i,j,k,l,last;

    for(i=0;i<row;i++)
    {
    //last contain the number of stars in last row
    last=topstar - 2*i;

    if(last<2)
     break;

     for(k=0;k<i;k++)
       printf("_");
    for(j=0;j<last;j++)
       printf("*");
    for(l=0;l<i;l++)
       printf(" ");
       printf("\n");
   }
  return 0;
}
#包括
int main()
{
//topstar在第一行有多少颗星星
int行=7;
int-topstar=7;
int i,j,k,l,last;

对于(i=0;i使用此C代码作为参考。它将打印到行数或最后一行开始数小于2时,以较早者为准

    #include<stdio.h>
    int main()
    {
    //topstar have the number of stars in top row
    int row=7;
    int topstar=7;
    int i,j,k,l,last;

    for(i=0;i<row;i++)
    {
    //last contain the number of stars in last row
    last=topstar - 2*i;

    if(last<2)
     break;

     for(k=0;k<i;k++)
       printf("_");
    for(j=0;j<last;j++)
       printf("*");
    for(l=0;l<i;l++)
       printf(" ");
       printf("\n");
   }
  return 0;
}
#包括
int main()
{
//topstar在第一行有多少颗星星
int行=7;
int-topstar=7;
int i,j,k,l,last;

对于(i=0;i使用此C代码作为参考。它将打印到行数或最后一行开始数小于2时,以较早者为准

    #include<stdio.h>
    int main()
    {
    //topstar have the number of stars in top row
    int row=7;
    int topstar=7;
    int i,j,k,l,last;

    for(i=0;i<row;i++)
    {
    //last contain the number of stars in last row
    last=topstar - 2*i;

    if(last<2)
     break;

     for(k=0;k<i;k++)
       printf("_");
    for(j=0;j<last;j++)
       printf("*");
    for(l=0;l<i;l++)
       printf(" ");
       printf("\n");
   }
  return 0;
}
#包括
int main()
{
//topstar在第一行有多少颗星星
int行=7;
int-topstar=7;
int i,j,k,l,last;
对于(i=0;i
spaces=0;
asterix=toprow;
对于(行=1;行<((toprow/2)+(toprow%2))&&asterix>1;行++)
{
对于(i=0;i
空格=0;
asterix=toprow;
对于(行=1;行<((toprow/2)+(toprow%2))&&asterix>1;行++)
{
对于(i=0;i
空格=0;
asterix=toprow;
对于(行=1;行<((toprow/2)+(toprow%2))&&asterix>1;行++)
{
对于(i=0;i
空格=0;
asterix=toprow;
对于(行=1;行<((toprow/2)+(toprow%2))&&asterix>1;行++)
{
对于(i=0;i
从第一个数字中连续减去2,然后使用if语句检查其是否大于或等于2。但是,格式如何?滞后空格。从第一个数字中连续减去2,然后使用if语句检查其是否大于或等于2。但是,格式如何?滞后空格。contin从第一个数字中减去2,然后使用if语句检查其是否大于或等于2。但是,格式如何?滞后空格。从第一个数字中连续减去2,然后使用if语句检查其是否大于或等于2。但是,格式如何?滞后空格。嗯……我正在尝试是,但我没有得到任何输出。看起来如果输入是偶数行,它不会打印最后一行(**)。我需要额外的位来包含奇数行的最后一行。通过编辑,例如,对于
toprow=5
rows=2
,它只打印5个星号。编辑:Nevermind,你是对的。我忘记了
=
。再次感谢。嗯……我正在尝试,但没有得到任何输出。看起来输入是偶数对于行,它不打印最后一行(**)。我需要额外的位来包含奇数行的最后一行。通过编辑,例如,对于
toprow=5
rows=2
,它只打印5个星号。编辑:Nevermind,你是对的。我忘记了
=
。再次感谢。嗯……我正在尝试,但没有得到任何输出。看起来输入是偶数对于行,它不打印最后一行(**)。我需要额外的位来包含奇数行的最后一行。通过编辑,例如,对于
toprow=5
rows=2
,它只打印5个星号。编辑:Nevermind,你是对的。我忘记了
=
。再次感谢。嗯……我正在尝试,但没有得到任何输出。看起来输入是偶数对于行,它不打印最后一行(**)。我需要额外的位来包含奇数行的最后一行。在编辑时,对于
toprow=5
rows=2
,它处于启用状态