java 输出一个爱心_for语句 输出心形(java)

论坛 期权论坛 脚本     
已经匿名di用户   2021-11-19 14:38   2520   0

展开全部

代码如下:

// 分三个大部分 上中下

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

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

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();//换行

}

运行效果图:

扩展资料:

另一种输出心形代码:

public class PrintHeart {

public static void main(String[] args) {

System.out.println(printHeart("*"));

}

private static String printHeart(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();

}

}

打印结果:

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:81
帖子:4969
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP