编写一个C++程序,使用for循环打印空心矩形星形图案。
#include<iostream>
using namespace std;
int main()
{
int i, j, rows, columns;
cout << "Enter Hollow Rectangle Rows = ";
cin >> rows;
cout << "Enter Hollow Rectangle Columns = ";
cin >> columns;
cout << "Hollow Rectangle Star Pattern\n";
for(i = 0; i < rows; i++)
{
for(j = 0; j < columns; j++)
{
if (i == 0 || i == rows - 1 || j == 0 || j == columns - 1)
{
cout << "*";
}
else
{
cout << " ";
}
}
cout << "\n";
}
return 0;
}

这个C++示例使用while循环打印给定字符的空心矩形图案。
#include<iostream>
using namespace std;
int main()
{
int i = 0, j, rows, columns;
char ch;
cout << "Enter Hollow Rectangle Rows and Columns = ";
cin >> rows >> columns;
cout << "Enter Symbol for Hollow Rectangle = ";
cin >> ch;
cout << "Hollow Rectangle Star Pattern\n";
while( i < rows)
{
j = 0;
while(j < columns)
{
if (i == 0 || i == rows - 1 || j == 0 || j == columns - 1)
{
cout << ch;
}
else
{
cout << " ";
}
j++;
}
cout << "\n";
i++;
}
return 0;
}
Enter Hollow Rectangle Rows and Columns = 14 25
Enter Symbol for Hollow Rectangle = #
Hollow Rectangle Star Pattern
#########################
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
#########################