心形java
1. 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);
}
}
2. 用java或者c程序輸出如下圖的這種簡單心形,誰能給我講講思路。加上程序示例更好
用方程的話肯定輸出不會和你的圖完全一致
心形線方程如圖
3. for語句 輸出心形(java)
for語句輸出心形(java)的方法如下:
使用eclipse,創建一個Java文件命名為Test.java
(3)心形java擴展閱讀:
for 語句表達式:
表達式1:一般為賦值表達式,給控制變數賦初值;
表達式2:關系表達式或邏輯表達式,循環控制條件;
表達式3:一般為賦值表達式,給控制變數增量或減量。
4. ❤️的代碼怎麼寫
摘要 您好,以下是總結望採納
5. 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);
}
}
6. Java 心形圖形
public class Heart {
public static void main(String[] args) {
int size = 4;
int number = 7;
for (int i = 0; i <= size; i++) {
for (int j = 4; j <= 4 - i; j--)//變數j一直自減1 最後是負數的時候也滿足條件 所以無限循環了
System.out.print(" ");
for (int j = 1; j <= i; j++)//
System.out.print("*");
for (int j = size; j <= size - i; j--)//變數j一直自減1 最後是負數的時候也滿足條件 所以無限循環了
System.out.print(" ");
for (int j = 1; j <= i; j++)
System.out.print("*");
System.out.println();
}
for (int k = number; k <= number; k--) {
System.out.print("*");
System.out.println();
}
}
}
7. 如何用JAVA語言編寫一個心形代碼
1,首先用圖紙畫一個心形。
2,使用方框,把每筆,做成一個點。
3,輸出這些點。就可以了。
希望能幫到你
8. 如何用JAVA語言編寫一個心形代碼用循環語句
1,首先用圖紙畫一個心形。 2,使用方框,把每筆,做成一個點。 3,輸出這些點。就可以了。 希望能幫到你
9. 如何用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 結果有點像愛心 還湊合著 你看看怎麼樣吧 哈
10. java心形代碼
把如下代碼加入<body>區域中
<style type="text/css">
body
{
background-color:#FFFFFF;
font-family: Arial;
font-size:10pt;
}
</style>
<script>
text=new Array('www','njcatv','net','需要放大的
/\/\
\ /
\/
文字')
var numText=4
color=new Array('#000000','#2F2F2F','#4F4F4F','#7F7F7F','#408080')
var numColors=5
var endSize=70
var Zspeed=30
var Cspeed=200
var font='Arial Black'
var hide=false
var size=10
var gonum=0
var ie, n;
if (document.all) {
n=0
ie=1
zoomText='document.all.zoom.innerText=text[num]'
zoomSize='document.all.zoom.style.fontSize=size'
closeIt=""
fadeColor="document.all.zoom.style.color=color[num]"
}
if (document.layers) {
n=1;ie=0
zoomText=""
zoomSize="document.zoom.document.write('<p align=\"center\" style=\"font-family:'+font+'; font-size:'+size+'px; color:'+color[0]+'\">'+text[num]+'</p>')"
closeIt="document.zoom.document.close()"
fadeColor="document.zoom.document.write('<p align=\"center\" style=\"font-family:'+font+'; font-size:'+endSize+'px; color:'+color[num]+'\">'+text[numText-1]+'</p>')"
}
function zoom(num,fn){
if (size<endSize){
eval(zoomText)
eval(zoomSize)
eval(closeIt)
size+=5;
setTimeout("zoom("+num+",'"+fn+"')",Zspeed)
}else{
eval(fn);
}
}
function fadeIt(num){
if (num<numColors){
eval(fadeColor)
eval(closeIt)
num+=1;
setTimeout("fadeIt("+num+")",Cspeed)
}else{
hideIt()
}
}
function hideIt(){
if(hide){
if(ie)document.all.zoom.style.visibility="hidden"
if(n)document.layers.zoom.visibility="hidden"
}
}
function init(){
if(ie){
document.all.zoom.style.color=color[0]
document.all.zoom.style.fontFamily=font
}
if(ie || n) go(0)
}
function go(num){
gonum+=1
size=10
if(num<numText){
zoom(num,'go('+gonum+')')
}else{
fadeIt(0)
}
}
</script>
<div id="zoom" align="center" style="position:absolute; top:150">