繁体   English   中英

我需要使用嵌套for循环在Java中创建菱形图案的帮助

[英]I need help creating a diamond pattern in java using nested for loops

这是我的代码:

public class Diamond {

   public static void main(String[] args) {


        int n = 8;
          for(int i=0; i<n; i++) {

            for(int k=n-1; k>i ;k--) {
                System.out.print(" ");
            }

            for(int j=0; j<1*i+1; j++) {
                System.out.print("/");
            }

            for(int j=1; j<=1; j++) {
                System.out.print("*");
            }

            for(int j=0; j<1*i+1; j++) {
                System.out.print("\\");
            }

            System.out.println(" ");
        }
    }
}

这应该是我的结果:

在此处输入图片说明

提示这是您必须知道每一行中的字符数。 还要注意“ *”索引的位置。 您还必须将钻石分为上半部分和下半部分。 然后,根据以下知识进行循环:“ *”之前和之后的是“ /”或“ \\”,并且“ /”或“ \\”的数目将在每次循环后增加。

这是示例代码。

/**
 *
 *
 */
public class Diamond {

    public static void main(String[] args) {

        int diamondHeight = 18;
        int diamondWidth = 21;
        String header = "";
        header = makeDiamondTemplate(diamondWidth, '+', '-', '-', '-');
        System.out.println(header);
        makeUpperHalf(diamondHeight / 2, diamondWidth);
        makeLowerHalf(diamondHeight / 2, diamondWidth);
        System.out.println(header);
        makeLowerHalf(diamondHeight / 2, diamondWidth);
        makeUpperHalf(diamondHeight / 2, diamondWidth);
        System.out.println(header);
    }

    public static String makeDiamondTemplate(int diamondWidth, char border, char mid, char fillerLeft,
            char fillerRight) {
        String result = "" + border;
        int midIndex = diamondWidth / 2;
        for (int i = 1; i < diamondWidth - 1; i++) {
            if (midIndex == i) {
                result = result + mid;
            } else if (i < midIndex) {
                result = result + fillerLeft;
            } else if (i > midIndex) {
                result = result + fillerRight;
            }
        }
        result = result + border;

        return result;
    }

    public static void makeUpperHalf(int diamondHeight, int diamondWidth) {
        StringBuilder template = new StringBuilder(makeDiamondTemplate(diamondWidth, '|', '*', ' ', ' '));
        int starIndex = diamondWidth / 2;
        System.out.println(template);
        for (int i = 1; i < diamondHeight; i++) {
            template.setCharAt(starIndex - i, '/');
            template.setCharAt(starIndex + i, '\\');
            System.out.println(template);
        }
    }

    public static void makeLowerHalf(int diamondHeight, int diamondWidth) {
        StringBuilder template = new StringBuilder(makeDiamondTemplate(diamondWidth, '|', '*', '\\', '/'));
        int starIndex = diamondWidth / 2;
        int replaceStart = starIndex - 2;
        if (template.length() > 1) {
            template.setCharAt(1, ' ');
            template.setCharAt(template.length() - 2, ' ');
        }
        System.out.println(template);
        for (int i = 1; i < diamondHeight; i++) {
            template.setCharAt(starIndex - replaceStart, ' ');
            template.setCharAt(starIndex + replaceStart, ' ');
            replaceStart--;
            System.out.println(template);

        }
    }

}

暂无
暂无

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

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