使用for循環打印愛心


 

 代碼如下

public class Heart {
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();
}

}

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM