题目:
//while循环实现
int main()
{
int n;
cout << "请输入边长:";
cin >> n;
int i = 1;
while (i <= n)
{ // 控制行数
int j = 1;
while (j <= n)
{ // 控制列数
if (i == 1 || i == n || j == 1 || j == n)
{
cout << "*";
}
else
{
cout << " ";
}
j++;
}
cout << endl;
i++;
}
return 0;
}
//for循环实现(更简单)
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "请输入边长:";
cin >> n;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if (i == 1 || i == n || j == 1 || j == n)
{
cout << "*";
}
else
{
cout << " ";
}
}
cout<<endl;
}
return 0;
}