题解 | #菱形图案#
菱形图案
https://www.nowcoder.com/practice/8b935f8ffe99445dbd42494febd13e45
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while(scan.hasNextInt()) {
int n = scan.nextInt();
int len = 2 * n + 1;
//循环2n+1次
for(int i = 0; i < len; i++){
//打印上三角
if (i < len / 2) {
for (int k = 0; k < len/2-i; k++) {
System.out.print(" ");
}
for(int j = 0; j <= i; j++){
System.out.print("* ");
}
}
//打印下三角
else if (i > len / 2) {
for (int k = 0; k < i-(len/2); k++) {
System.out.print(" ");
}
for(int j = 0; j < len-i; j++){
System.out.print("* ");
}
}
//打印中间
else {
for (int j = 0; j <= len / 2; j++) {
System.out.print("* ");
}
}
System.out.println();
}
}
}
}
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while(scan.hasNextInt()) {
int n = scan.nextInt();
int len = 2 * n + 1;
//循环2n+1次
for(int i = 0; i < len; i++){
//打印上三角
if (i < len / 2) {
for (int k = 0; k < len/2-i; k++) {
System.out.print(" ");
}
for(int j = 0; j <= i; j++){
System.out.print("* ");
}
}
//打印下三角
else if (i > len / 2) {
for (int k = 0; k < i-(len/2); k++) {
System.out.print(" ");
}
for(int j = 0; j < len-i; j++){
System.out.print("* ");
}
}
//打印中间
else {
for (int j = 0; j <= len / 2; j++) {
System.out.print("* ");
}
}
System.out.println();
}
}
}
}