Java语言打印心型

一、今天闲来无事找了两个打印心形的java代码(撩妹可用哦)

—————————————————————第一种————————————————————————

心形

public static void main(String[] args) {
		System.out.println(callBack("*"));
	}

	public static String callBack(String input) {

		int[] array = { 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1,
				0, 0, 1, 1, 4, 5, 2, 3, 4, 1, 0, 1,

				0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
				0, 0, 0, 0, 0, 0 };

		StringBuffer sb = new StringBuffer();

		for (int i = 0; i < array.length; i++) {

			if (i % 7 == 0)

				sb.append("  \n");

			if (array[i] == 0)

				sb.append("   ");

			else if (array[i] == 4)

				sb.append("  ");

			else if (array[i] == 5)

				sb.append(" I ");

			else if (array[i] == 2)

				sb.append("Lvoe ");

			else if (array[i] == 3)

				sb.append("You");

			else

				sb.append("  " + input);

		}

		return sb.toString();

	}

———————————————————第二种——————————————————————

 

public static void main(String[] args) {
		// 分三个大部分 上中下

		for (int i = 0, k = 0; i < 14; i++) {// 打印行

			// 上部分 上分为 四个部分

			if (i < 3) {

				for (int j = 0; j < 5 - 2 * i; j++) {// 1、空心

					System.out.print(" ");

				}

				if (i == 2) {// 2、*

					for (int j = 0; j < 6 + 4 * i - 1; j++) {

						System.out.print("*");

					}

					for (int j = 0; j < 7 - 4 * i + 2; j++) {// 3、空心

						System.out.print(" ");

					}

					for (int j = 0; j < 6 + 4 * i - 1; j++) {// 4、*

						System.out.print("*");

					}

				} else {

					for (int j = 0; j < 6 + 4 * i; j++) {// 2、*

						System.out.print("*");

					}

					for (int j = 0; j < 7 - 4 * i; j++) {// 3、空心

						System.out.print(" ");

					}

					for (int j = 0; j < 6 + 4 * i; j++) {// 4、*

						System.out.print("*");

					}

				}

			} else if (i < 6) {// 中间

				for (int j = 0; j < 29; j++) {

					System.out.print("*");

				}

			} else {// 下部分 6

				if (i == 13) {

					for (int j = 0; j < 2 * (i - 6); j++) {// 打印空格

						System.out.print(" ");

					}

					System.out.print("*");

				} else {

					for (int j = 0; j < 2 * (i - 6) + 1; j++) {// 打印空格

						System.out.print(" ");

					}

					for (int j = 1; j < 28 - 4 * k; j++) {

						System.out.print("*");

					}

					k++;

				}

			}

			System.out.println();// 换行

		}
	}
发布了12 篇原创文章 · 获赞 9 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/finaly_yu/article/details/83479242
今日推荐