打印 * 图案在C++
Printing * pattern in C++
我正在尝试打印这样的图案
*******
* *
* *
* *
* *
* *
*******
在这里,它应该看起来像一个空盒子。但不知何故,我没有更接近
到目前为止,我对此进行了编码
#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<<"*";
}`
- 如何循环打印顶点结构
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 如何在c++中打印目录
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 在线编译器中的分段C++没有打印消息
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 如何将结构插入到集合中并打印集合的成员
- 在循环C++中指定字符串之后,不会打印该字符串
- 以螺旋方式打印矩阵的程序.(工作不好)
- 从控制台中删除最后打印的元素
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 如何仅使用对象名称打印特定于对象的成员
- 回溯C++不打印函数,因此文件
- 在一定长度后从数组中打印时缺少整数
- 为什么这个 c++ 代码打印出长度 5,当我打印出字符串时,程序会自动终止?
- 在gem5中打印文件中的所有cache_blocks
- 打印数字图案
- Log4cpp:以UTC/GMT时区打印日期
- 如何使用gdb制作一个可以漂亮地打印每个对象的C++函数