桃走 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 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景