[Java] 纯文本查看 复制代码
public class Heart {
public static void main(String[] args) {
for(int x=1;x<=5;x++){
for(int y=1;y<=7;y++){
if(x==1&&y==3){
System.out.print("**");
}
else if(x==1&&y==5){
System.out.print("**");
}
else if(x==2&&y==2){
System.out.print("**");
}
else if(x==2&&y==4){
System.out.print("**");
}
else if(x==2&&y==6){
System.out.print("**");
}
else if(x==3&&y==2){
System.out.print("**");
}
else if(x==3&&y==6){
System.out.print("**");
}
else if(x==4&&y==3){
System.out.print("**");
}
else if(x==4&&y==5){
System.out.print("**");
}
else if(x==5&&y==4){
System.out.print("**");
}
else
System.out.print(" ");
}
System.out.println("");
}
}
}
[Java] 纯文本查看 复制代码
import java.util.Scanner;
public class FinallyHeart {
public static void main(String[] args) {
while(true){
System.out.println("请输入数字..输入end结束");
String str = new Scanner(System.in).next();
if(str.equals("end")){
System.out.println("系统结束");
break;
}
printHeart(Integer.parseInt(str));
}
}
public static void printHeart(int n){
for(int x=1;x<=2+3*n;x++){
for(int y=1;y<=1+4*n;y++){
if(x==1){
if(y==1+n||y==3*n+1){
System.out.print("**");
continue;
}
}else if(1<x&&x<n+1){
if(y==n+x||y==3*n+x||y==2+n-x||y==3*n+2-x){
System.out.print("**");
continue;
}
}else if(x==1+n){
if(y==1||y==1+2*n||y==4*n+1){
System.out.print("**");
continue;
}
}else if(n+1<x&&x<2+3*n){
if(y==x-n-1||y==5*n+3-x){
System.out.print("**");
continue;
}
}else if(x==2+3*n){
if(y==1+2*n){
System.out.print("**");
continue;
}
}
System.out.print(" ");
}
System.out.println("");
}
}
}