桃走
2021-08-13 19:09
采纳率: 50%
浏览 34
已结题

求如何用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:
++++++++++++
+++++--+++++
++++----++++
+++------+++
++--------++
+----------+
+----------+
++--------++
+++------+++
++++----++++
+++++--+++++
++++++++++++

  • 收藏

3条回答 默认 最新

  • 已采纳
    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();
    }
    
    已采纳该答案
    打赏 评论
  • 托马斯-酷涛 2021-08-13 19:10

    多个循环嵌套

    打赏 评论
  • moonlight_Answer 2021-08-13 21:21

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

    打赏 评论

相关推荐 更多相似问题