以下是使用 Java 代码实现爱心的示例:

public class Heart {
    public static void main(String[] args) {
        int x, y;
        double z;
        int size = 20; // 爱心大小
        char[][] heart = new char[size][size];

        // 初始化数组
        for (x = 0; x < size; x++) {
            for (y = 0; y < size; y++) {
                heart[x][y] = ' ';
            }
        }

        // 绘制爱心
        for (double i = 0; i < 2 * Math.PI; i += 0.01) {
            z = Math.pow(Math.pow(Math.sin(i), 2) + Math.pow(Math.cos(i), 2.5) - 1, 3) - Math.pow(Math.sin(i), 2) * Math.pow(Math.cos(i), 3);
            x = (int) (size / 4 + z * Math.cos(i) * size / 4);
            y = (int) (size / 2 - z * Math.sin(i) * size / 4);
            heart[x][y] = '*';
        }

        // 打印爱心
        for (x = 0; x < size; x++) {
            for (y = 0; y < size; y++) {
                System.out.print(heart[y][x]);
            }
            System.out.println();
        }
    }
}

运行代码后,将会输出一个绘制好的爱心图案。

Java 代码绘制爱心图案 - 简单易懂的实现