求如何用Java中的for循环打印这个图形

求如何用Java中的for循环打印这个图形

问题描述:

Write a program that displays the following pattern using nested for-loops.
REQUIREMENTS
·         Your code must use nested for-loops.
·         Your program should use only the following 3 output statements, one of EACH of the followings:
  System.out.print("+"); // print +
  System.out.print("-"); // print -
  System.out.println(); // print a newline
·         The user input is always correct (input verification is not required).
·         Your code must work exactly like the following example (the text in bold indicates the user input).
 
Example of the program:

  • Example 1:

Enter the number of rows: 3
The pattern is as follows:
++++++
++--++
+----+
+----+
++--++
++++++

  • Example2:

Enter the number of rows: 6
The pattern is as follows:
++++++++++++
+++++--+++++
++++----++++
+++------+++
++--------++
+----------+
+----------+
++--------++
+++------+++
++++----++++
+++++--+++++
++++++++++++

int i,j,n = 3;
for (i = n; i >=1; i--) {
    for (j = 1; j <= i; j++)
        System.out.print("+");
    for (j = 1; j <= (n-i)*2; j++)
        System.out.print("-");
    for (j = 1; j <= i; j++)
        System.out.print("+");
    System.out.println();
}
for (i = 1; i <= n; i++) {
    for (j = 1; j <= i; j++)
        System.out.print("+");
    for (j = 1; j <= (n-i)*2; j++)
        System.out.print("-");
    for (j = 1; j <= i; j++)
        System.out.print("+");
    System.out.println();
}

多个循环嵌套

上下平分,每一部分都是循环,然后上面和下面那部分中间等分成两部分,然后再分符号,+号一个循环,-号一个循环,四个小循环,两个大循环,然后嵌套