创建双镜像三角形 [英] Creating a double mirrored triangle

查看:161
本文介绍了创建双镜像三角形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助制作这样的镜像三角形:

I need help making a mirrored triangle like this:

*      *
**    **
***  ***
********

我可以单独得到每一个,但我不能将它们组合起来。

I can get each one seperatly, but I can't combine them.

public static void main(String[] args){
      for( int i = 1; i <= 5; i++ ){
            for( int j = 0; j < i; j++ ){
                System.out.print("*");

            }
            System.out.println();
        }
      for(int i = 0; i < 6; i++)
      {
          for(int j = 5; j > 0; j--)
          {
              if(i < j)
                  System.out.print(" ");
              else
                  System.out.print("*");
          }
          System.out.println();
      }
}


推荐答案

你可以使用此代码执行此操作。

You can do it using this code.

public class Test {
    public void sizeOfTri(int triSize) { //Number of lines
        int line = 1;
        for(int i = 0; i < triSize; i++) { //Handles Each line
            int temp = triSize * 2;
            for(int j = 1; j < temp; j++) { //Handles Each space on the line
                if(j <= line && j == triSize || j >= temp - line && j == triSize) { //For the very last line because it needs an extra *
                    System.out.print("**");
                } else if(j <= line || j >= temp - line) { //For normal lines
                    System.out.print("*");
                } else if(j == triSize) {
                    System.out.print("  "); //For the second to last line because it needs an extra space to make it look mirrored
                } else { //For normal lines
                    System.out.print(" ");
                }
            }
            System.out.println();
            line++;
        }
    }

    public static void main(String[] args) {
        new Test().sizeOfTri(4);
    }
}

我在if语句的旁边评论了哪一部分做了什么。运行时会产生如下所示的输出

I commented on next to if statements on which part does what. This will produce an output which looks like the below when run

*      *
**    **
***  ***
********

这篇关于创建双镜像三角形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆