用用户输入为基础,以 ASCII 艺术风格绘制一个心形。

huangapple go评论116阅读模式
英文:

Draw a heart in ASCII-art style based on user input

问题

以下是翻译好的部分:

我有一个问题,希望你能帮忙。

这个Java程序应该以ASCII艺术风格打印出心形,基于用户输入的N

信息:

  • 要打印的字符:♡ ♥
  • 心形的顶部底部部分可以分开制作。
  • 解决方案必须基于for循环。
  • N 决定了心形的顶部部分:
    1. 顶部的倾斜外侧有N个心形
    2. 顶部的平坦部分有N个心形
    3. 两个平坦部分之间的间隙宽度为N个心形

示例:

我目前的代码:

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("请输入要打印的心形数量");
    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个。

问题:

  • 如何让不同类型的心形在每一行都完成它们的“工作”?
英文:

I have a problem that I hope you can help with.

The Java program should print hearts in ASCII-art style, based on the input N given by a user.

Information:

  • Characters to print: ♡ ♥
  • The printing of the top and bottom part of the heart can be done separately.
  • The solution must be based on for-loops.
  • N determines the top part of the heart:
    1. The slanted outer sides at the top have N hearts in them.
    2. The flat parts at the top have N hearts in them.
    3. The gap between the two flat parts is N hearts wide.

Examples:

My current code:

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

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

Thoughts:

  • The first line of the heart is based on:
    2 * (userInput - 1) + 3 * userInput
    
  • The colored hearts must increase by 2 for each line.
  • The center hearts must be reduced by 2 for each line.
  • The transparent hearts on the side must be reduced by 1 for each line.

Questions:

  • How can I get the different types of hearts to do their "job" on each line?

答案1

得分: 4

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();
}
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();
}

Output 1:

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

Output 2:

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


<details>
<summary>英文:</summary>

### Draw an ASCII-art heart, filled and empty

Imagine a *coordinate plane* and draw a figure that consists of two half-сircles and one half-rhombus.

![two half-сircles and one half-rhombus][1]

Pictures 1 and 2:

![ASCII-art heart filled][2] ![ASCII-art heart empty][3]

Figure 1 - [Try it online!][4]

```java
int n = 6;
for (int y = -n; y &lt;= 2 * n; y++) {
    for (int x = -2 * n; x &lt;= 2 * n; x++)
        if ((y &lt;= 0 &amp;&amp;
                ((int) Math.sqrt((x+n)*(x+n) + y*y) &lt;= n
                        || (int) Math.sqrt((x-n)*(x-n) + y*y) &lt;= n))
                || (y &gt; 0 &amp;&amp; Math.abs(x) &lt;= 2 * n - y))
            System.out.print(&quot;♥ &quot;);
        else
            System.out.print(&quot;♡ &quot;);
    System.out.println();
}

Output 1:

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

Figure 2 - [Try it online!][5]

int n = 6;
for (int y = -n; y &lt;= 2 * n; y++) {
    for (int x = -2 * n; x &lt;= 2 * n; x++)
        if ((y &lt;= 0 &amp;&amp;
                ((int) Math.sqrt((x+n)*(x+n) + y*y) == n
                        || (int) Math.sqrt((x-n)*(x-n) + y*y) == n))
                || (y &gt; 0 &amp;&amp; Math.abs(x) == 2 * n - y))
            System.out.print(&quot; &quot;);
        else
            System.out.print(&quot; &quot;);
    System.out.println();
}

Output 2:

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

<sup>See also:
<br>• [Print out an ASCII circle and axes with characters][6]
<br>• [How to draw a double empty ASCII rhombus?][7]</sup>

1: https://i.stack.imgur.com/kXZZ5.jpg "two half-сircles and one half-rhombus"
2: https://i.stack.imgur.com/W0I2z.jpg "ASCII-art heart filled"
3: https://i.stack.imgur.com/XCzJG.jpg "ASCII-art heart empty"
4: https://tio.run/##jZGxbsIwEIb3PMUvBmQnclR16JLSN2BirDoYCNQ0OCE2yFbhQTp26Iv1RVLbBJQ0ouoNd9bd/3/2yRt@4KyscrlZvjVNtZ8XYoFFwZXClAuJ9wgu2r7SXLtyKMUSWzclM10LuX5@Aa/XirZiH0JqSEzwkF1bq7IG8X3r@kxmrj5OcI8Y/pwkXXtPb7y@1ZmOxzhPzxEuXoGQQL7DeDwYX4J4MnUr6tdU7WpNiEkkjUNGAhtb6hnyJuASxyOGKBZQrI@iNPoLYvEUnnwG8bkihl6XBYO94Z9ZpfNtWu51WrnP0GT0/fGFEc0G6rxQ@b8RnwPEb1UhSUdxis751DQ/ "ASCII-art heart filled"
[5]: https://tio.run/##jZGxTsMwEIb3PMWvDpWdyBFiYAnhDZg6Iga3TYvb1AmxW9mifRBGBl6MFwm2m6KEqIgb7qy7///skzf8wFlVF3Kz3LZtvZ@XYoFFyZXCIxcSbxFcdH2luXblUIkldm5KZroRcv30DN6sFe3EPoTUkMhxl/20VlUD4vvW9ZnMXL3PcYsY/pwkfftAb7y@05mexzjPwBEuXoGQQL7BdDoaX4J4MnUr6pdUvTaaEJNIGoeMBDa2FHkOeRVwieMRYxQLKDZEURr9BbF4CE8@g/hcERN8YVkw2Cv@mVW62KXVXqe1@wxNJl/vn5jQbKQuSlX8G/ExQvxWlZL0FKfonE9t@w0 "ASCII-art heart empty"
[6]: https://stackoverflow.com/a/66844335/16314724
[7]: https://stackoverflow.com/a/68155637/16314724

答案2

得分: 0

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);
    }
}

In my output it is not a perfect rectangle as the white heart is a little smaller than the colored heart.


<details>
<summary>英文:</summary>
Nice challenge:
```java
import java.util.Scanner;
public class A {
public static final char COLORED = &#39;♥&#39;;
public static final char WHITE = &#39;♡&#39;;
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 &gt; 0) {
for (int i = 0; i &lt; outerHearts; i++) {
row.append(WHITE);
}
for (int i = 0; i &lt; heartsSection1; i++) {
row.append(COLORED);
}
for (int i = 0; i &lt; innerHearts; i++) {
row.append(WHITE);
}
for (int i = 0; i &lt; heartsSection2; i++) {
row.append(COLORED);
}
for (int i = 0; i &lt; outerHearts; i++) {
row.append(WHITE);
}
row.append(&quot;\n&quot;);
if (upperPart) {
if (outerHearts &gt; 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 &lt;= 0)
break;
}
System.out.println(row.toString());
}
public static void main(String args[]) {
System.out.println(&quot;Enter the number of hearts you want to print&quot;);
Scanner scan = new Scanner(System.in);
int userInput = scan.nextInt();
printTop(userInput);
}
}

In my output it is not a perfect rectangle as the white heart is a little smaller than the colored heart.

答案3

得分: 0

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

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 drawMiddleOfHeart() {
        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;

            drawMiddleOfHeart();
            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();
    }
}

希望这对你有帮助。

英文:

I solved the task with the following code:

import java.util.Scanner;

public class ChristmasHearts {
    private int userInput;
    private int height;
    private int width;
    String topHeart = &quot;&quot;;
    String middleHeart = &quot;&quot;;
    String bottomHeart = &quot;&quot;;

    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 &lt; heightOfTopHeart; i++) {
                this.topHeart += &quot;&quot;;
                this.topHeart += &quot;&quot;;
                this.topHeart += &quot;&quot;;
            }
            System.out.println(topHeart);
        } else {
            whites = this.userInput - 1;
            blacks = this.userInput;
            center = this.userInput;
            heightOfTopHeart = this.userInput - 1;

            for (int i = 0; i &lt; heightOfTopHeart; i++) {
                for (int j = 0; j &lt; whites; j++) {
                    this.topHeart += &quot;&quot;;
                }

                for (int j = 0; j &lt; blacks; j++) {
                    this.topHeart += &quot;&quot;;
                }

                for (int j = 0; j &lt; center; j++) {
                    this.topHeart += &quot;&quot;;
                }

                if (this.userInput &gt;= 5 &amp;&amp; center &lt;= 0) {
                    if (isEven(this.userInput) &amp;&amp; center == 0) {
                        for (int j = 0; j &lt; blacks; j++) {
                            this.topHeart += &quot;&quot;;
                        }
                    } else if (isEven(this.userInput) &amp;&amp; center &lt; 0) {
                        int rightSideBlacks = blacks - isEvenCenterLessSubtractor;

                        for (int j = 0; j &lt; rightSideBlacks; j++) {
                            this.topHeart += &quot;&quot;;
                        }
                        isEvenCenterLessSubtractor += 2;
                    } else if (!isEven(this.userInput) &amp;&amp; center &lt; 0) {
                        int rightSideBlacks = blacks - isNotEvenCenterLessSubtractor;

                        for (int j = 0; j &lt; rightSideBlacks; j++) {
                            this.topHeart += &quot;&quot;;
                        }
                        isNotEvenCenterLessSubtractor += 2;
                    } else {
                        int factorToSubtract = 2;
                        int rightSideBlacks = blacks - factorToSubtract;

                        for (int j = 0; j &lt; rightSideBlacks; j++) {
                            this.topHeart += &quot;&quot;;
                        }
                        factorToSubtract += 2;
                    }
                } else {
                    for (int j = 0; j &lt; blacks; j++) {
                        this.topHeart += &quot;&quot;;
                    }
                }

                for (int j = 0; j &lt; whites; j++) {
                    this.topHeart += &quot;&quot;;
                }

                whites -= 1;
                blacks += 2;
                center -= 2;

                if (this.userInput == 0) {
                    this.topHeart = &quot;&quot;;
                }
                System.out.println(topHeart);
                topHeart = &quot;&quot;;
            }
        }
    }

    private void drawMiddelOfHeart() {
        for (int j = 0; j &lt; this.width; j++) {
            this.middleHeart += &quot;&quot;;
        }

        if (this.userInput == 0) {
            this.middleHeart = &quot;&quot;;
        }

        System.out.println(this.middleHeart);
        middleHeart = &quot;&quot;;
    }

    private void drawBottomOfHeart() {
        int whites = 1;
        int blacks = 1;
        int heightOfBottomHeart = 1;

        if (this.userInput == 1) {
            for (int i = 0; i &lt; heightOfBottomHeart; i++) {
                this.bottomHeart += &quot;&quot;;
                this.bottomHeart += &quot;&quot;;
                this.bottomHeart += &quot;&quot;;
            }
            System.out.println(bottomHeart);

        } else {
            heightOfBottomHeart = this.height - this.userInput;
            whites = 1;
            blacks = this.width - 2;

            drawMiddelOfHeart();
            for (int i = 0; i &lt; heightOfBottomHeart; i++) {

                for (int j = 0; j &lt; whites; j++) {
                    this.bottomHeart += &quot;&quot;;
                }

                for (int j = 0; j &lt; blacks; j++) {
                    this.bottomHeart += &quot;&quot;;
                }

                for (int j = 0; j &lt; whites; j++) {
                    this.bottomHeart += &quot;&quot;;
                }

                blacks -= 2;
                whites += 1;

                if (this.userInput == 0) {
                    this.bottomHeart = &quot;&quot;;
                }
                System.out.println(bottomHeart);
                bottomHeart = &quot;&quot;;
            }
        }
    }

    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 &lt;= (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();
    }
}

答案4

得分: 0

public static void main(String[] args) {
  int[][] hearts = stringToPixels("♥♡", new Font(Font.SERIF, Font.PLAIN, 22), 0);
  // 输出
  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 要绘制的源字符串
 * @param f   用于渲染文本的字体
 * @param pd  作为预防的填充,在大多数情况下为0
 * @return 由组成此字符串的像素构成的二维数组
 */
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;
}
英文:

You can use the java.awt package to draw these characters ♥♡ as a black and white picture:

用用户输入为基础,以 ASCII 艺术风格绘制一个心形。

And then you can print a 2D array of pixels as characters that make up this picture:

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

Try it online!

public static void main(String[] args) {
  int[][] hearts = stringToPixels(&quot;♥♡&quot;, 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 ? &quot; &quot; : &quot;  &quot;);
    if (sb.indexOf(&quot;&quot;) &gt; -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, &quot;png&quot;, new File(&quot;str.png&quot;));
  int[][] pcx = new int[height][width];
  for (int i = 0; i &lt; height; i++)
    for (int j = 0; j &lt; width; j++)
      if (image.getRGB(j, i) == 0xFFFFFFFF)
        pcx[i][j] = 1;
  return pcx;
}

1: https://i.stack.imgur.com/9VIvz.png "characters ♥♡ as a black and white picture"
2: https://tio.run/##bVTNctowEL7zFFufbHBEwrGUtqFNMsy0SYZwyTBMR9iyETG2R5KDM508SI459MXyInQly8SG6KCf/dPq20@7po/0JMtZug4fdju@yTOhYI1CQreKdIedQ1GUpYpc4jRlacjED9yxUh3b8Q2NGRkXUcQECyf6NOx08mKZ8ACChEoJvylP4W8HcFi5VFTh8pjxEDaode@U4Gk8XwAVsfSssR48VfMFyleMCiVhhK7acpbd8pIl0nXeXv69vbw6PqRsCzpfV0/k7mI6ufSNgNz@Op9c@zAYeD6cesN97H4fskLlhdpLokyAa64EkW3hs722mZAeVbbjgieIDMglpqVvb4ndxkXN0MAxLAb3Wlo95JLQHCsUuhw@jeAUvgG@7hUcdHAAnIOAPAIXXThWp7yJNBCvjgdf4eTsOPTdk1RsQ/C1JMccVZKiayPec6eazdLvditFF77nVNCNxhxkVoiAWfhBZRAKuj2wi8w78Y2oFoY2oElzYJWHyAMahjoOlUAhFyygheJZ6mO9YZNJBQGVTMLp3lUwVYgU1IrB4CeyRNAnyCLIDQtQTBUS6YFBkeOBS5unde@b1ZKuZtQBkariaWnFGoh0MgqzbVb/6ENAoEpb/iOdG5GYqZmgqcTqb1ykX0QTyezSwH/KAkXTOGGwzIo01Dw3vpZRRuaa1PA6T2usrBFDJ7vloVqhcxWFVMeeRrwLg7bpivF4pd5t7fnYuPW1wXx3GB0xTAPQsnTN5b69x28ryez@9uLP@H6G0@T6fHrfeMeVoPmKBxLLHNstZum@i70qCY1CLWziUDsRyZTpB9FHSs3eCt0KV3x1r4aiNMeT@vjUahkm/8kN2QqumGsy8cHJ07huQTxhroMxiZZ57QIZ4uVBzRgtqfBZzA1ci2Hng36BvWCIyxeLJe57Pe/j5rKujNdobALi9tC27h17EKdXY3eNZPdghM7lpR3HXqaBB@WcL@brBV509p6t/Z6oHdpW8rzb/Qc "Java (OpenJDK 8) – Try It Online"

答案5

得分: 0

以下是已经翻译好的内容:

最简单的方法是为行构建一个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.............
英文:

The easiest is to build a char[] for the line, fill it with blank hearts, then fill the sides with white hearts, and calculate and fill the center with white hearts.

The result is very concise and fast code, but the center calculation might seem complex, when though it's a simple formula.

public static void printHeart(int n) {
	printHeart(n, &#39;\u2665&#39;, &#39;\u2661&#39;);
}

public static void printHeart(int n, char black, char white) {
	if (n &lt;= 0)
		return;
	int width = 5 * n - 2;
	char[] buf = new char[width];
	for (int i = 1 - n; i &lt; (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 &gt; 0)
			Arrays.fill(buf, (width - center) / 2, (width + center) / 2, white);
		System.out.println(buf);
	}
}

Notice how the code doesn't need separate sections for printing top and bottom halfs. This is a trick achieved by iterating i from negative to positive numbers, e.g. for n=4, i is iterated from -3 to +8, both inclusive.

Tests

Since the font on my browser don't use hearts of equal width, the result is bad, so the following test uses regular characters.

for (int n = 0; n &lt;= 6; n++)
	printHeart(n, &#39;X&#39;, &#39;.&#39;);

Outputs

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.............

huangapple
  • 本文由 发表于 2020年9月17日 16:27:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63934144.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定