简体   繁体   English

使用java在菱形图案中打印菱形图案

[英]Printing a diamond pattern in a diamond pattern using java

I want to print out a pattern like this but I can't get it to work. 我想打印出这样的图案,但我无法让它工作。 The big problem I'm having is the lines outside the diamond and the parts in the diamond where there is just spaces. 我遇到的一个大问题是钻石外面的线条和钻石中只有空间的部分。 I have marked my output white different numbers to mark areas so i know what part who is doing what. 我已经标记了我的输出白色不同的数字来标记区域,所以我知道什么部分谁在做什么。 I hope someone can help please. 我希望有人可以帮忙。

Here is my code: 这是我的代码:

public static void main(String[] args) {

        int rows1 = 10;
        System.out.println("## Printing the pattern ##");

        for (int i=1; i<=rows1; i++)
        {
            // Print space in decreasing order övre väster hörn tomrum
            for (int j=rows1; j>i; j--)
            {
                System.out.print(" 1 ");
            }


            // Print star in increasing order övre inre delen av triangeln tomrum
            for (int k=1; k<=(i * 2) -1; k++)
            {
                if( k == 1 || k == (i * 2) -1)
                    System.out.print("*");
                else
                    System.out.print(" 2 ");
            }
            System.out.println();
        }
        for (int i=rows1-1; i>=1; i--)
        {
         // Print space in increasing order nedre västra härnet tomrum
            for (int j=rows1-1; j>=i; j--)
            {
                System.out.print(" 3 ");
            }
            // Print star in decreasing order nedre inre delen av triangeln
            for (int k=1; k<=(i * 2) -1; k++)
            {
                if( k == 1 || k == (i * 2) -1 )
                    System.out.print("*");
                else
                    System.out.print(" 4 ");
            }


            System.out.println();
        }

}

How I want it to look or near it 我希望它看起来或接近它

This class prints the image in the picture. 该类在图片中打印图像。 Basic idea is that you use the (left part of) the middle line of the diamond, to create the rest of the form. 基本的想法是你使用钻石中间线的(左侧部分)来创建表格的其余部分。

public class Diamond {

    private String base;

    public Diamond(String base) {
        this.base = base;
    }

    private String reverse(String s) {
        return new StringBuilder(s).reverse().toString();
    }

    public void print() {

        int l = base.length();
        int c = (l/2)-1;

        String[] diamond = new String[l-1];
        diamond[c] = base + " " + reverse(base);

        String next = "/ " + base;
        for (int i = 1; i < l/2; i++) {

            String center = " ";
            if (next.endsWith("  ") == false) {
                center = "O";
            }

            next = next.substring(0, l);
            String bottom = next.replace('/', '\\');
            String bottomRight = reverse(next);
            String right = bottomRight.replace('/', '\\');

            diamond[c-i] = next + center + right;
            diamond[c+i] = bottom + center + bottomRight;

            next = "  " + next;
        }

        for (String line : diamond) {
            System.out.println(line);
        }
    }

    public static void main(String[] args) {
        new Diamond("  O O O O O     O O O       ").print();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM