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

求如何用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();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 8月22日
  • 已采纳回答 8月14日
  • 创建了问题 8月13日

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器