JAVA打印爱心

JAVA循环输出打印爱心

源代码:


public class aixin {
    public static void main(String args[]) {
        int n = 4;
        int x, y, z;
        for (x = 1; x <= n; x++) {
            for (y = 1; y <= 2 * (n - x) - 1; y++) {
                System.out.print(" ");
            }
            for (y = 1; y <= 6 + (10 - x) * (x - 1) / 2; y++) {
                System.out.print("*");
            }
            for (y = 1; y <= 2 * n - 1 + (x - 6) * (x - 1); y++) {
                if (x == n)
                    break;
                System.out.print(" ");
            }
            for (y = 1; y <= 6 + (10 - x) * (x - 1) / 2; y++) {
                if (x== n)
                    break;
                System.out.print("*");
            }
            if (x == n) {
                for (y = 1; y <= 6 + (10 - x) * (x - 1) / 2 - 1; y++) {
                    System.out.print("*");
                }
            }
            System.out.println("");
        }
        for (x = 1; x <= n - 2; x++) {
            for (y = 1; y <= 6 + (10 - n) * (n - 1) / 2 - 1 + 6 + (10 - n) * (n - 1) / 2; y++) {
                System.out.print("*");
            }
            System.out.println("");
        }
        z = 6 + (10 - n) * (n - 1) / 2 + 6 + (10 - n) * (n - 1) / 2 - 1;
        for (x = 1; x <= (z - 2 - 3) / 4 + 1; x++) {
            for (y = 1; y <= 2 * x - 1; y++) {
                System.out.print(" ");
            }
            for (y = 1; y <= z + 2 - 4 * x; y++) {
                System.out.print("*");
            }
            System.out.println("");
        }
        for (y = 1; y <= 2 * ((z - 2 - 3) / 4 + 1); y++)

        {
            System.out.print(" ");
        }
        System.out.print("*");
        System.out.println();
        System.out.println();
        System.out.println();
    }

}

运行截图:

JAVA打印爱心_第1张图片

 

你可能感兴趣的:(JAVA,java,开发语言)