打印 * 图案在C++

Printing * pattern in C++

本文关键字:C++ 打印      更新时间:2023-10-16

我正在尝试打印这样的图案

*******
*     *
*     *
*     *
*     *
*     *
*******

在这里,它应该看起来像一个空盒子。但不知何故,我没有更接近

到目前为止,我对此进行了编码

#include <iostream>
using namespace std;
int main( int argc, char ** argv ) {
for(int i=1;i<=7;i++)
{
    for(int j=1;j<=7;j++)
    {
        if(j==1||j==7)
        printf("*");
        else printf(" ");
    }
    printf("n");
}
return 0;
}

我的输出是

*     *
*     *
*     *
*     *
*     *
*     *
*     *

只有 for 循环会很好

if(j==1||j==7)
printf("*");
else printf(" ");

此逻辑适用于除第一行和最后一行之外的所有行。因此,您必须考虑行值并对第一行和最后一行进行特殊检查。这两个没有空格。

[假设这是家庭作业,我只是给出一个提示。你几乎已经做到了,上面的提示应该足以让它工作。

您的if条件只需要:

if (i==1 || i==7 || j==1 || j==7)

也就是说,您需要检查您是否在第一行或最后一行以及第一列或最后一列上,然后打印*

你离得很近。 问题出在这一行:

    if(j==1||j==7)

更改它,使其也考虑顶部和底部行。

祝你好运

您需要在第一行和最后一行采取不同的行为:

int W = 7, H = 7;
for(int i=0;i<=H;i++)
{
  for(int j=0;j<=W;j++)
  {
     // if row is the first or row is the last or column is the first of column is the last
     if (i == 0 || i == H-1 || j == 0 || j == W-1)
       printf("*");
     else printf(" ");
  }
  printf("n");
}

这个函数可以正常工作:

#include <iostream>
#include <string>
void printBox(int width, int height) {
    if (width < 2 or height < 1) return;
    for (int i = 1; i <= height; i++) {
        if (i == 1 or i == height) {
            std::string o(width, '*');
            std::cout << o << std::endl;
        } else {
            std::string o(width-2, ' ');
            o = '*' + o + '*';
            std::cout << o << std::endl;
        }
    }
}

它可以用作:

printBox(2, 2);

其中打印:

**
**

或作为:

printBox(6, 4);

其中打印:

******
*    *
*    *
******

由于我不是编程专家,我想出了这个简单的代码:

#include <stdio.h>
int main(void)
{
    int i,j,k,n;
    printf("Enter no of rows : n");
    scanf("%d", &n);
    for(i=0; i<n; i++)
    {
        if(i == 0 || i == (n-1))
        {
            for(j=0; j <n-1; j++)
                printf("*");
        }              
        else
        {
            for(k=0; k<n; k++)
            {
                if (k == 0 || k == (n-2))
                    printf("*");
                else
                    printf(" ");
            }
        }
        printf("n");
    }
    return 0; 
}
for(int j=1;j<=7;j++)
{
    if(j==1||j==7)
        printf("*******n");
    else
        printf("*     *n");
}
printf("n");

您平等地对待所有行,而忽略了第一行和最后一行必须以不同方式处理的事实。你需要类似的东西

if (i == 1 || i == 7)
{
    for (j=1;j<7;j++) printf("*");
    printf("n");
}
else { /* your routine */ }

这是你的代码应该的样子:

#include <iostream>
using namespace std;
int main( int argc, char ** argv ) {
for(int i=1;i<=7;i++)
{
    for(int j=1;j<=7;j++)
    {
        if(j == 1 || j == 7)
           printf("*");
        else if (i == 1 || i == 7 ) //added this check
             printf ("*");
        else printf(" ");
    }

    printf("n");
}
return 0;
}

现场示例

你需要

一个嵌套循环。因为您有两个选项介于两者之间,所以您需要两个嵌套循环。一个用于空格,一个用于迭代 1 和 7(0 和 6(中的填充"*"。

打印第 1 行和第 7 行,并用"*"填充边界星。用

if (i == 0 || i == 7)  // in-case you're initializing i with 0
    // loop for printf ("*");

正如其他人所说,您需要处理第一行和最后一行。构建每行的一种方法是使用字符串构造函数之一,因此无需编写内部循环。

#include <string>
#include <iostream>
using namespace std;
int main()
{
    int height = 7, width = 7;
    for( int i=0; i<height; i++ )
    {
        char c = (i == 0 || i == height-1) ? '*' : ' ';
        string line(width, c);
        line[0] = line[width-1] = '*';
        cout << line << endl;
    }
    return 0;
}
#include <iostream>
#include<iomanip>
using namespace std;
main()
{
      int Width;
      char MyChar;
      int LCV;    //Loop control
      int LCVC, LCVR;    //Loop control for LCVC=Columns LCVR=Rows
      cout<<"nEnter Width: "; cin>>Width;
      cout<<"nEnter a Character: "; cin>>MyChar;
      for (LCV=1;LCV<=Width;LCV++)
      {cout<<MyChar;}
      cout<<endl;
      for(LCVC=1;LCVC<=Width;LCVC++)
      { cout<<MyChar<<setw(Width-1)<<MyChar<<endl;
      }
      for (LCV=1;LCV<=Width;LCV++)
      {cout<<MyChar;}
      cout<<endl;
      system("pause");
}
/*
Enter Width: 6
Enter a Character: #
######
#    #
#    #
#    #
#    #
#    #
#    #
######
Press any key to continue . . .
*/

打印此模式输入一个数字=每个数字,例如 23517**



*


然后 ** * ***** * *******然后** ** ****** *


和 * * * * *

我想出了一个简单的解决方案。您可以保持非常简单,而不是使用您使用的字符数组指针。

#include <iostream>
using namespace std;
int main() {
    int rows = 7;
    for(int i = 1; i<=rows;i++){
        if(i == 1 || i == rows){
            cout<<"*******"<<endl;
        }else{
            cout<<"*     *"<<endl;
        }
    }
    return 0;
}   // This code outputs 7 rows with 7 characters in 1st and 7 line each and 1 each character at the start and last position of the remaining rows

但是如果你想要一个自定义的行数,你可以尝试以下代码

#include <iostream>
using namespace std;
int main() {
    int rows;
    cout<<"Enter the number of rows: ";
    cin>>rows;  // gets input rows from the user
    for(int i = 1; i<=rows;i++){
        if(i == 1 || i == rows){
            cout<<"*******"<<endl;
        }else{
            cout<<"*     *"<<endl;
        }
    }
    return 0;
}

您可以添加:

    `if(i==0 || i==6)
    {
    cout<<"*";
    }`