A. 如何用java在頁面上用循環列印一個愛心形狀
import java.util.Scanner;
public class test{
static void draw(int n)
{
int i,j;
for (i=1-(n>>1);i<=n;i++)
if (i>知仿拿0)
{
for (j=0;j<i;j++) System.out.print("大瞎 ");
for (j=1;j<=2*(n-i)+1;j++)
if (j==1||j==2*(n-i)+1) System.out.print(" *");
else System.out.print(" ");
System.out.println("\n");
}
else
if (i==0)
{
System.out.print(" *");
for (j=1;j<n;j++) System.out.print(" ");
System.out.print(" *");
for (j=1;j<n;j++) System.out.print(" ");
System.out.print(" *\n");
}
else
{
for (j=i;j<0;j++) System.out.print(" ");
for (j=1;j<=n+2*i+1;j++)
if (i==1-(n>>1)) System.out.print(" *"搭搭);
else if (j==1||j==n+2*i+1) System.out.print(" *");
else System.out.print(" ");
for (j=1;j<=-1-2*i;j++) System.out.print(" ");
for (j=1;j<=n+2*i+1;j++)
if (i==1-(n>>1)) System.out.print(" *");
else if (j==1||j==n+2*i+1) System.out.print(" *");
else System.out.print(" ");
System.out.print("\n");
}
}
public static void main(String[] args) {
System.out.println("Please input the size (n>=4):");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
draw(n);
}
}先說明這個不是我寫的 是看了有人用C語言寫的 就順便改成了java 結果有點像愛心 還湊合著 你看看怎麼樣吧 哈