java打印愛心語錄

解夢佬 0

1. for語句 輸出心形(java)

代碼如下: public class Demo7 { public static void main(String[] args){ int i=0,j=0; for(i=1;i<=3;i++){ for(j=1;j<=3-i;j++){ System.out.print(" "); //上左空格 } System.out.print(" * "); for(j=1;j<=2*i-3;j++){ System.out.print(" "); //上左心 } if(i!=1){ System.out.print(" * "); } for(j=5;j>2*i-2;j--){ System.out.print(" ");//上中空格 } System.out.print(" * "); for(j=1;j<=2*i-3;j++){ System.out.print(" "); //上右心 } if(i!=1){ System.out.print(" * "); } System.out.println(); } for(i=1;i<=3;i++){ if(i==2){ System.out.print(" *"); } for(j=1;j<=11;j++){ if(i==1&&j==7){ System.out.print(" * "); } System.out.print(" "); //中心 } if(i==2){ System.out.print(" * "); } System.out.println(); } for(i=1;i<=6;i++){ for(j=2;j<=1+i;j++){ System.out.print(" "); //下空格 } System.out.print(" * "); for(j=7;j>=2*i-3;j--){ System.out.print(" "); //下心 } if(i!=6){ System.out.print(" * "); } System.out.println(); } } } 。

2. java中怎樣打印心形

package com.zeph.j2se.alg;

public class Heart {

int HighLevel(int wide) {

int i = 0, j = 0, k = 0, t = 0, m = 0, n = 0, count = 1;// i控制循環內高度,j控制每行前面空格,k控制輸出的*數

// t控制高層星號中間空格,m記錄高層最底行的星數,以下逐行增4

// n記錄頂行空個數,以下逐行減4;count記錄高層高度,與high無關

m = (wide - 4) / 2;

do {

count++;

m -= 4;

} while (m > 4);

if ((wide - 4) % 2 == 0)// 區別對待奇偶寬度,奇數中間最小空1個,偶數最小空兩個

{

n = 2 + 4 * (count - 1);

m--;

} else

n = 1 + 4 * (count - 1);

for (i = 0; i < count; i++) {

for (j = (count - i) * 2; j > 0; j--)

System.out.print(" ");

for (k = 0; k < m; k++)

System.out.print("*");

for (t = 0; t < n; t++)

System.out.print(" ");

for (k = 0; k < m; k++)

System.out.print("*");

m += 4;

n -= 4;

System.out.print("\n");

}

return 0;

}

int LowLevel(int wide) {

int i = 0, j = 0, k = 0;// i控制輸出行,j控制輸出每行前的空格,k控制輸出*

int high = 0, tmp = wide;

do// 計算所需高度

{

high++;

tmp -= 4;

} while (tmp > 4);

high += 1;

for (i = 0; i < high; i++) {

for (j = 0; j < 2 * i; j++)

System.out.print(" ");

for (k = wide - 4 * i; k > 0; k--)

System.out.print("*");

System.out.print("\n");

}

return 0;

}

public static void main(String[] args) {

int wide = 30;

Heart heart = new Heart();

heart.HighLevel(wide);

heart.LowLevel(wide);

}

}

3. 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);

}

}

4. 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);

}

}

5. 如何用java在頁面上用循環打印一個愛心形狀

import java.util.Scanner;

public class test{

static void draw(int n)

{

int i,j;

for (i=1-(n>>1);i0)

{

for (j=0;j>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)) 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 結果有點像愛心 還湊合著 你看看怎麼樣吧 哈

6. 如何用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>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 結果有點像愛心 還湊合著 你看看怎麼樣吧 哈。

7. 我需要JAVA for循環語句打印菱形 和 正三角的代碼 希望能給加好注釋

/*

本項小程序用于打印一個菱形圖形

需求:

1.打印如下圖形

*

* *

* * *

* * * *

* * *

* *

*

思路:

1.需要兩個分離的循環

2.打印

*/

public class LingXing //這是本類的名字

{

public static void main(String[] args)

{

int m=10,n=m-1; //用戶定義要打印的菱形的大小

/*

這個for循環是用于控制打印笑臉的部分

*/

for(int x=1;x<=m;x++) //笑臉的行數

{

for(int y=x;y<=m;y++) //笑臉左面的空缺

{

System.out.print(" ");

}

for(int z=1;z<=x;z++) //笑臉的輸出部分

{

System.out.print("\2 ");

}

System.out.println();

}

/*

這個for循環是用于控制打印心形的部分

*/

for(int i=1;i<=n;i++) //心形的行數

{

for(int j=1;j<=i;j++) //心形左邊的空缺

{

System.out.print(" ");

}

for(int k=n;k>=i;k--) //心形的輸出部分

{

System.out.print(" \3");

}

System.out.println();

}

}

}

2.正三角形也是同理,沒很大差別:《代碼如上相似》