有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java基于用户输入以ASCIIart样式绘制心脏

我有个问题,希望你能帮我解决

Java程序应该根据用户给出的输入N以ASCII艺术风格打印心形

信息:

  • 要打印的字符:♡ ♥
  • 可以分别打印心脏的顶部底部部分
  • 解决方案必须基于for循环
  • N决定心脏的顶部:
    1. 顶部倾斜的外侧有N颗心
    2. 顶部的扁平部分有N个心形
    3. 两个扁平零件之间的间隙为N

示例:

我的当前代码:

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter the number of hearts you want to print");
    int userInput = scan.nextInt();

    printTop(userInput);
}
public static void printTop(int userInput) {
    String row = "";
    int width = 2 * (userInput - 1) + 3 * userInput;
    for (int height = 0; height < userInput; height++) {
        for (int i = 0; i < userInput - 1; i++) {
            row += "♡";
        }
        for (int i = 0; i < userInput; i++) {
            row += "♥";
        }
        for (int i = 0; i < userInput; i++) {
            row += "♡";
        }
        for (int i = 0; i < userInput; i++) {
            row += "♥";
        }
        for (int i = 0; i < userInput - 1; i++) {
            row += "♡";
        }
        row += "\n";
    }
    System.out.println(row);
}

想法:

  • 心脏的第一行是基于:
    2 * (userInput - 1) + 3 * userInput
    
  • 每行的彩色心形必须增加2
  • 每行中心心脏必须减少2
  • 侧面的透明心脏必须每行减少1

问题:

  • 我怎样才能让不同类型的心脏在每一条线上完成他们的“工作”

共 (5) 个答案

  1. # 1 楼答案

    画一个ASCII艺术心形,充满和空

    想象一个坐标平面,画一个由两个半圆形和一个半菱形组成的图形

    two half-сircles and one half-rhombus

    图1和图2:

    ASCII-art heart filledASCII-art heart empty

    图1-Try it online!

    int n = 6;
    for (int y = -n; y <= 2 * n; y++) {
        for (int x = -2 * n; x <= 2 * n; x++)
            if ((y <= 0 &&
                    ((int) Math.sqrt((x+n)*(x+n) + y*y) <= n
                            || (int) Math.sqrt((x-n)*(x-n) + y*y) <= n))
                    || (y > 0 && Math.abs(x) <= 2 * n - y))
                System.out.print("♥ ");
            else
                System.out.print("♡ ");
        System.out.println();
    }
    

    产出1:

    ♡ ♡ ♡ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♡ ♡ ♡ ♡ ♡ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♡ ♡ ♡ 
    ♡ ♡ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♡ ♡ ♡ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♡ ♡ 
    ♡ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♡ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♡ 
    ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ 
    ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ 
    ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ 
    ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ 
    ♡ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♡ 
    ♡ ♡ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♡ ♡ 
    ♡ ♡ ♡ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♡ ♡ ♡ 
    ♡ ♡ ♡ ♡ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♡ ♡ ♡ ♡ 
    ♡ ♡ ♡ ♡ ♡ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♡ ♡ ♡ ♡ ♡ 
    ♡ ♡ ♡ ♡ ♡ ♡ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♡ ♡ ♡ ♡ ♡ ♡ 
    ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♡ ♡ ♡ ♡ ♡ ♡ ♡ 
    ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ 
    ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ 
    ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♥ ♥ ♥ ♥ ♥ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ 
    ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♥ ♥ ♥ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ 
    ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♥ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ 
    

    图2-Try it online!

    int n = 6;
    for (int y = -n; y <= 2 * n; y++) {
        for (int x = -2 * n; x <= 2 * n; x++)
            if ((y <= 0 &&
                    ((int) Math.sqrt((x+n)*(x+n) + y*y) == n
                            || (int) Math.sqrt((x-n)*(x-n) + y*y) == n))
                    || (y > 0 && Math.abs(x) == 2 * n - y))
                System.out.print("♥ ");
            else
                System.out.print("♡ ");
        System.out.println();
    }
    

    产出2:

    ♡ ♡ ♡ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♡ ♡ ♡ ♡ ♡ ♥ ♥ ♥ ♥ ♥ ♥ ♥ ♡ ♡ ♡ 
    ♡ ♡ ♥ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♥ ♡ ♡ ♡ ♥ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♥ ♡ ♡ 
    ♡ ♥ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♥ ♡ ♥ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♥ ♡ 
    ♥ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♥ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♥ 
    ♥ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♥ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♥ 
    ♥ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♥ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♥ 
    ♥ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♥ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♥ 
    ♡ ♥ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♥ ♡ 
    ♡ ♡ ♥ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♥ ♡ ♡ 
    ♡ ♡ ♡ ♥ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♥ ♡ ♡ ♡ 
    ♡ ♡ ♡ ♡ ♥ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♥ ♡ ♡ ♡ ♡ 
    ♡ ♡ ♡ ♡ ♡ ♥ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♥ ♡ ♡ ♡ ♡ ♡ 
    ♡ ♡ ♡ ♡ ♡ ♡ ♥ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♥ ♡ ♡ ♡ ♡ ♡ ♡ 
    ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♥ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♥ ♡ ♡ ♡ ♡ ♡ ♡ ♡ 
    ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♥ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♥ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ 
    ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♥ ♡ ♡ ♡ ♡ ♡ ♥ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ 
    ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♥ ♡ ♡ ♡ ♥ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ 
    ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♥ ♡ ♥ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ 
    ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♥ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ 
    

    另请参见:
    Print out an ASCII circle and axes with characters
    How to draw a double empty ASCII rhombus?

  2. # 2 楼答案

    我用以下代码解决了这个任务:

    import java.util.Scanner;
    
    public class ChristmasHearts {
        private int userInput;
        private int height;
        private int width;
        String topHeart = "";
        String middleHeart = "";
        String bottomHeart = "";
    
        public ChristmasHearts(int input) {
            this.userInput = input;
        }
    
        public void draw() {
            calcWidthAndHeight(this.userInput);
            drawTopOfHeart();
            drawBottomOfHeart();
        }
    
        private void drawTopOfHeart() {
            int whites = 0;
            int blacks = 1;
            int center = 1;
            int heightOfTopHeart = 1;
            int isEvenCenterLessSubtractor = 2;
            int isNotEvenCenterLessSubtractor = 1;
    
            if (this.userInput == 1) {
                for (int i = 0; i < heightOfTopHeart; i++) {
                    this.topHeart += "♥";
                    this.topHeart += "♡";
                    this.topHeart += "♥";
                }
                System.out.println(topHeart);
            } else {
                whites = this.userInput - 1;
                blacks = this.userInput;
                center = this.userInput;
                heightOfTopHeart = this.userInput - 1;
    
                for (int i = 0; i < heightOfTopHeart; i++) {
                    for (int j = 0; j < whites; j++) {
                        this.topHeart += "♡";
                    }
    
                    for (int j = 0; j < blacks; j++) {
                        this.topHeart += "♥";
                    }
    
                    for (int j = 0; j < center; j++) {
                        this.topHeart += "♡";
                    }
    
                    if (this.userInput >= 5 && center <= 0) {
                        if (isEven(this.userInput) && center == 0) {
                            for (int j = 0; j < blacks; j++) {
                                this.topHeart += "♥";
                            }
                        } else if (isEven(this.userInput) && center < 0) {
                            int rightSideBlacks = blacks - isEvenCenterLessSubtractor;
    
                            for (int j = 0; j < rightSideBlacks; j++) {
                                this.topHeart += "♥";
                            }
                            isEvenCenterLessSubtractor += 2;
                        } else if (!isEven(this.userInput) && center < 0) {
                            int rightSideBlacks = blacks - isNotEvenCenterLessSubtractor;
    
                            for (int j = 0; j < rightSideBlacks; j++) {
                                this.topHeart += "♥";
                            }
                            isNotEvenCenterLessSubtractor += 2;
                        } else {
                            int factorToSubtract = 2;
                            int rightSideBlacks = blacks - factorToSubtract;
    
                            for (int j = 0; j < rightSideBlacks; j++) {
                                this.topHeart += "♥";
                            }
                            factorToSubtract += 2;
                        }
                    } else {
                        for (int j = 0; j < blacks; j++) {
                            this.topHeart += "♥";
                        }
                    }
    
                    for (int j = 0; j < whites; j++) {
                        this.topHeart += "♡";
                    }
    
                    whites -= 1;
                    blacks += 2;
                    center -= 2;
    
                    if (this.userInput == 0) {
                        this.topHeart = "";
                    }
                    System.out.println(topHeart);
                    topHeart = "";
                }
            }
        }
    
        private void drawMiddelOfHeart() {
            for (int j = 0; j < this.width; j++) {
                this.middleHeart += "♥";
            }
    
            if (this.userInput == 0) {
                this.middleHeart = "";
            }
    
            System.out.println(this.middleHeart);
            middleHeart = "";
        }
    
        private void drawBottomOfHeart() {
            int whites = 1;
            int blacks = 1;
            int heightOfBottomHeart = 1;
    
            if (this.userInput == 1) {
                for (int i = 0; i < heightOfBottomHeart; i++) {
                    this.bottomHeart += "♡";
                    this.bottomHeart += "♥";
                    this.bottomHeart += "♡";
                }
                System.out.println(bottomHeart);
    
            } else {
                heightOfBottomHeart = this.height - this.userInput;
                whites = 1;
                blacks = this.width - 2;
    
                drawMiddelOfHeart();
                for (int i = 0; i < heightOfBottomHeart; i++) {
    
                    for (int j = 0; j < whites; j++) {
                        this.bottomHeart += "♡";
                    }
    
                    for (int j = 0; j < blacks; j++) {
                        this.bottomHeart += "♥";
                    }
    
                    for (int j = 0; j < whites; j++) {
                        this.bottomHeart += "♡";
                    }
    
                    blacks -= 2;
                    whites += 1;
    
                    if (this.userInput == 0) {
                        this.bottomHeart = "";
                    }
                    System.out.println(bottomHeart);
                    bottomHeart = "";
                }
            }
        }
    
        private boolean isEven(int n) {
            return n % 2 == 0;
        }
    
        private void calcWidthAndHeight(int n) {
            this.width = 3;
            this.height = 2;
    
            for (int counter = 1; counter <= (n - 1); counter++) {
                this.width += 5;
    
                if (isEven(counter)) {
                    this.height += 4;
                } else {
                    this.height += 3;
                }
            }
        }
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int input = scanner.nextInt();
    
            scanner.close();
    
            ChristmasHearts heart = new ChristmasHearts(input);
            heart.draw();
        }
    }
    
  3. # 3 楼答案

    您可以使用java.awt包将这些字符♥♡绘制为黑白图片:

    picture

    然后你可以打印一个2D像素阵列作为组成这张图片的字符:

            ♡ ♡ ♡ ♡         ♡ ♡ ♡ ♡                 ♡ ♡ ♡ ♡         ♡ ♡ ♡           
          ♡ ♡ ♡ ♡ ♡ ♡     ♡ ♡ ♡ ♡ ♡ ♡             ♡         ♡     ♡       ♡         
        ♡ ♡ ♡ ♡ ♡ ♡ ♡   ♡ ♡ ♡ ♡ ♡ ♡ ♡           ♡           ♡   ♡           ♡       
        ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡         ♡             ♡ ♡             ♡     
        ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡         ♡             ♡               ♡     
        ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡         ♡                             ♡     
        ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡             ♡                           ♡     
          ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡             ♡                         ♡       
            ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡                 ♡                     ♡         
            ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡                     ♡                 ♡           
              ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡                       ♡                 ♡           
                ♡ ♡ ♡ ♡ ♡ ♡ ♡ ♡                         ♡             ♡             
                  ♡ ♡ ♡ ♡ ♡ ♡                             ♡         ♡               
                    ♡ ♡ ♡ ♡                                 ♡     ♡                 
                      ♡ ♡                                   ♡   ♡                   
                      ♡                                       ♡                     
    

    Try it online!

    public static void main(String[] args) {
      int[][] hearts = stringToPixels("♥♡", new Font(Font.SERIF, Font.PLAIN, 22), 0);
      // output
      for (int[] row : hearts) {
        StringBuilder sb = new StringBuilder();
        for (int i : row)
          sb.append(i != 0 ? "♡ " : "  ");
        if (sb.indexOf("♡") > -1)
          System.out.println(sb);
      }
    }
    
    /**
     * @param str source string to draw
     * @param f   font to render text
     * @param pd  padding as a precaution, in most cases 0
     * @return the 2D array of pixels that make up this string
     */
    static int[][] stringToPixels(String str, Font f, int pd) {
      FontRenderContext ctx = new FontRenderContext(f.getTransform(), false, false);
      Rectangle bounds = f.getStringBounds(str, ctx).getBounds();
      int width = bounds.width + pd * 2;
      int height = bounds.height + pd * 2;
      BufferedImage image =
              new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
      Graphics2D graphics = (Graphics2D) image.getGraphics();
      graphics.setFont(f);
      graphics.drawString(str, pd + bounds.x, pd - bounds.y);
      //ImageIO.write(image, "png", new File("str.png"));
      int[][] pcx = new int[height][width];
      for (int i = 0; i < height; i++)
        for (int j = 0; j < width; j++)
          if (image.getRGB(j, i) == 0xFFFFFFFF)
            pcx[i][j] = 1;
      return pcx;
    }
    
  4. # 4 楼答案

    最简单的方法是为线条构建一个char[],用空白的心填充,然后用白色的心填充侧面,然后计算并用白色的心填充中心

    结果是非常简洁和快速的代码,但中心计算可能看起来很复杂,尽管它是一个简单的公式

    public static void printHeart(int n) {
        printHeart(n, '\u2665', '\u2661');
    }
    
    public static void printHeart(int n, char black, char white) {
        if (n <= 0)
            return;
        int width = 5 * n - 2;
        char[] buf = new char[width];
        for (int i = 1 - n; i < (width + 1) / 2; i++) {
            int outer = Math.abs(i), center = 2 - n - 2 * i;
            Arrays.fill(buf, black);
            Arrays.fill(buf, 0, outer, white);
            Arrays.fill(buf, width - outer, width, white);
            if (center > 0)
                Arrays.fill(buf, (width - center) / 2, (width + center) / 2, white);
            System.out.println(buf);
        }
    }
    

    请注意,代码不需要单独的部分来打印上半部分和下半部分。这是通过将i从负数迭代到正数来实现的技巧,例如,对于n=4i-3迭代到+8,两者都包括在内

    测试

    由于我浏览器上的字体没有使用等宽的字体,结果很糟糕,所以下面的测试使用常规字符

    for (int n = 0; n <= 6; n++)
        printHeart(n, 'X', '.');
    

    输出

    X.X
    .X.
    
    .XX..XX.
    XXXXXXXX
    .XXXXXX.
    ..XXXX..
    ...XX...
    
    ..XXX...XXX..
    .XXXXX.XXXXX.
    XXXXXXXXXXXXX
    .XXXXXXXXXXX.
    ..XXXXXXXXX..
    ...XXXXXXX...
    ....XXXXX....
    .....XXX.....
    ......X......
    
    ...XXXX....XXXX...
    ..XXXXXX..XXXXXX..
    .XXXXXXXXXXXXXXXX.
    XXXXXXXXXXXXXXXXXX
    .XXXXXXXXXXXXXXXX.
    ..XXXXXXXXXXXXXX..
    ...XXXXXXXXXXXX...
    ....XXXXXXXXXX....
    .....XXXXXXXX.....
    ......XXXXXX......
    .......XXXX.......
    ........XX........
    
    ....XXXXX.....XXXXX....
    ...XXXXXXX...XXXXXXX...
    ..XXXXXXXXX.XXXXXXXXX..
    .XXXXXXXXXXXXXXXXXXXXX.
    XXXXXXXXXXXXXXXXXXXXXXX
    .XXXXXXXXXXXXXXXXXXXXX.
    ..XXXXXXXXXXXXXXXXXXX..
    ...XXXXXXXXXXXXXXXXX...
    ....XXXXXXXXXXXXXXX....
    .....XXXXXXXXXXXXX.....
    ......XXXXXXXXXXX......
    .......XXXXXXXXX.......
    ........XXXXXXX........
    .........XXXXX.........
    ..........XXX..........
    ...........X...........
    
    .....XXXXXX......XXXXXX.....
    ....XXXXXXXX....XXXXXXXX....
    ...XXXXXXXXXX..XXXXXXXXXX...
    ..XXXXXXXXXXXXXXXXXXXXXXXX..
    .XXXXXXXXXXXXXXXXXXXXXXXXXX.
    XXXXXXXXXXXXXXXXXXXXXXXXXXXX
    .XXXXXXXXXXXXXXXXXXXXXXXXXX.
    ..XXXXXXXXXXXXXXXXXXXXXXXX..
    ...XXXXXXXXXXXXXXXXXXXXXX...
    ....XXXXXXXXXXXXXXXXXXXX....
    .....XXXXXXXXXXXXXXXXXX.....
    ......XXXXXXXXXXXXXXXX......
    .......XXXXXXXXXXXXXX.......
    ........XXXXXXXXXXXX........
    .........XXXXXXXXXX.........
    ..........XXXXXXXX..........
    ...........XXXXXX...........
    ............XXXX............
    .............XX.............
    
  5. # 5 楼答案

    不错的挑战:

    import java.util.Scanner;
    
    public class A {
        public static final char COLORED = '♥';
        public static final char WHITE = '♡';
    
        public static void printTop(int userInput) {
            StringBuffer row = new StringBuffer();
    
            int width = 2 * (userInput - 1) + 3 * userInput;
    
            // white hearts at the outside
            int outerHearts = userInput - 1;
            // left colored section or complete
            // colored line from the widest point
            int heartsSection1 = userInput;
            // right color section or 0 from the widest point
            int heartsSection2 = userInput;
            // white hearts between 2 colored sections
            int innerHearts = userInput;
    
            boolean upperPart = true;
            while (heartsSection1 > 0) {
                for (int i = 0; i < outerHearts; i++) {
                    row.append(WHITE);
                }
                for (int i = 0; i < heartsSection1; i++) {
                    row.append(COLORED);
                }
                for (int i = 0; i < innerHearts; i++) {
                    row.append(WHITE);
                }
                for (int i = 0; i < heartsSection2; i++) {
                    row.append(COLORED);
                }
                for (int i = 0; i < outerHearts; i++) {
                    row.append(WHITE);
                }
                row.append("\n");
                if (upperPart) {
                    if (outerHearts > 0) {
                        outerHearts--;
                        innerHearts = Math.max(0, innerHearts - 2);
                        if (outerHearts == 0) {
                            heartsSection1 = width;
                            heartsSection2 = 0;
                        } else {
                            heartsSection1 += 2;
                            heartsSection2 += 2;
                        }
                    } else {
                        // line with only colored hearts,
                        // from now on the hearts reduce
                        upperPart = false;
                    }
                }
                if (!upperPart) {
                    outerHearts++;
                    heartsSection1 -= 2;
                }
                if (heartsSection1 <= 0)
                    break;
            }
            System.out.println(row.toString());
        }
    
        public static void main(String args[]) {
            System.out.println("Enter the number of hearts you want to print");
            Scanner scan = new Scanner(System.in);
            int userInput = scan.nextInt();
    
            printTop(userInput);
        }
    }
    

    在我的输出中,它不是一个完美的矩形,因为白色的心比彩色的心小一点