![](https://i-blog.csdnimg.cn/blog_migrate/82939e75c502f27c99eabdd101b2bc86.png)
代码:
#include "stdafx.h"
#include<iostream>
using namespace std;
#include<iostream>
using namespace std;
int main() {
int n,i;
cout << "输入几阶菱形n:";
cin >> n;
if (n>1&&n<10) {
for (i = 1; i <= n; i++) {
for (int j = 1; j <= 2 * n - 1; j++) {
if (j > n - i && j < n + i) {
cout << "*";
}
else {
cout << " ";
}
}
cout << endl;
}
for (i = n - 1; i > 0; i--) {
for (int j = 1; j <= 2 * n - 1; j++) { //控制列个数
if (j > n - i && j < n + i) {
cout << "*";
}
else {
cout << " ";
}
}
cout << endl;
}
}else {
cout << "超出范围!";
}
int n,i;
cout << "输入几阶菱形n:";
cin >> n;
if (n>1&&n<10) {
for (i = 1; i <= n; i++) {
for (int j = 1; j <= 2 * n - 1; j++) {
if (j > n - i && j < n + i) {
cout << "*";
}
else {
cout << " ";
}
}
cout << endl;
}
for (i = n - 1; i > 0; i--) {
for (int j = 1; j <= 2 * n - 1; j++) { //控制列个数
if (j > n - i && j < n + i) {
cout << "*";
}
else {
cout << " ";
}
}
cout << endl;
}
}else {
cout << "超出范围!";
}
return 0;
}
}
扩充:
代码:
![](https://i-blog.csdnimg.cn/blog_migrate/6292c3500fc631c465d8e13954411b5e.png)
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
int main()
{
int n;
char chose;
cout << "输入10以内几阶菱形;";
cin >> n;
cout << "实心选A,空心选B,你的选择是:";
cin >> chose;
switch (chose)
{
case 'A':
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= 2 * n - 1; j++) {
if (j > n - i && j < n + i) {
cout << "*";
}
if (j <= n - i || j >= n + i) {
cout << " ";
}
{
int n;
char chose;
cout << "输入10以内几阶菱形;";
cin >> n;
cout << "实心选A,空心选B,你的选择是:";
cin >> chose;
switch (chose)
{
case 'A':
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= 2 * n - 1; j++) {
if (j > n - i && j < n + i) {
cout << "*";
}
if (j <= n - i || j >= n + i) {
cout << " ";
}
}
cout << endl;
}
for (int i = n - 1; i > 0; i--) {
for (int j = 1; j <2 * n - 1; j++) {
if (j <= n - i || j >= n + i) {
cout << " ";
}
if (j > n - i && j < n + i) {
cout << "*";
}
}
cout << endl;
}
break;
case 'B':
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= 2 * n - 1; j++) {
if (j !=n-i+1 && j !=n+i-1) {
cout << " ";
}
else
{
cout << "*";
}
cout << endl;
}
for (int i = n - 1; i > 0; i--) {
for (int j = 1; j <2 * n - 1; j++) {
if (j <= n - i || j >= n + i) {
cout << " ";
}
if (j > n - i && j < n + i) {
cout << "*";
}
}
cout << endl;
}
break;
case 'B':
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= 2 * n - 1; j++) {
if (j !=n-i+1 && j !=n+i-1) {
cout << " ";
}
else
{
cout << "*";
}
}
cout << endl;
}
for (int i = n - 1; i > 0; i--) {
for (int j = 1; j <2 * n - 1; j++) {
if (j != n - i + 1 && j != n + i - 1) {
cout << " ";
}
else
{
cout << "*";
}
}
cout << endl;
}
break;
}
if (n <= 10) {
cout << endl;
}
for (int i = n - 1; i > 0; i--) {
for (int j = 1; j <2 * n - 1; j++) {
if (j != n - i + 1 && j != n + i - 1) {
cout << " ";
}
else
{
cout << "*";
}
}
cout << endl;
}
break;
}
if (n <= 10) {
}
else {
cout << "越界";
}
return 0;
}
else {
cout << "越界";
}
return 0;
}