十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
用方程的话肯定输出不会和你的图完全一致
成都创新互联是一家专注于成都网站制作、网站建设、外贸网站建设与策划设计,广河网站建设哪家好?成都创新互联做网站,专注于网站建设10多年,网设计领域的专业建站公司;建站业务涵盖:广河等地区。广河做网站价格咨询:13518219792
心形线方程如图
用上面行第二个比较简单,程序如下
#include stdio.h
bool draw(float x, float y)
{
float a = x * x + y * y - 1.0;
float b = x * x * y * y * y;
return a * a * a - b = 0;
}
int main(int argc, char* argv[])
{
for (float y = 1.5; y = -1.5; y -= 0.1)
{
for (float x = -1.2; x = 1.2; x += 0.05)
{
if (draw(x, y))
{
bool left = draw(x - 0.05, y);
bool right = draw(x + 0.05, y);
bool up = draw(x, y + 0.1);
bool down = draw(x, y - 0.1);
if (left right up down)
printf(" ");
else
printf("*");
}
else
printf(" ");
}
printf("\n");
}
return 0;
}
draw函数是判断(x,y)坐标是否在心形范围内
主函数循环,y取值±1.5之间,步长0.1,x取值±1.2之间,步长0.05
如果(x,y)坐标在心形范围外打印空格
如果(x,y)坐标在心形范围内,由于这个心是空心的,要继续判断是不是心形边缘,判断周围4个点坐标,如果都在范围内,表示(x,y)坐标不会是边缘,打印空格,否则是边缘,打印星号
最终结果如图
import java.util.Scanner;
public class test{
static void draw(int n)
{
int i,j;
for (i=1-(n1);i=n;i++)
if (i0)
{
for (j=0;ji;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;jn;j++) System.out.print(" ");
System.out.print(" *");
for (j=1;jn;j++) System.out.print(" ");
System.out.print(" *\n");
}
else
{
for (j=i;j0;j++) System.out.print(" ");
for (j=1;j=n+2*i+1;j++)
if (i==1-(n1)) 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-(n1)) 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 结果有点像爱心 还凑合着 你看看怎么样吧 哈
凑合着看吧,刚实现了一个,代码如下:
public class Test {
public static void main(String args[]) {
System.out.println("this is XX!");
System.err.println(" ** ** ");
System.err.println(" ** ** ** ** ");
System.err.println(" ** ** ** ");
System.err.println(" ** ** ");
System.err.println(" ** ** ");
System.err.println(" ** ** ");
System.err.println(" ** ");
}
}
复制到百度里面有点乱,你要是会用MyEclipse的话直接Ctrl+Shift+F;
如果喜欢的话希望采纳@~
用java代码输出心的形状
public class CartesianCurve extends Applet{
int width, height;
Image image;
Graphics draw_Curve;
public void init(){
setBackground(Color.black);
this.setSize(350, 310);
width = getSize().width;
height = getSize().height;
image = createImage(width, height);
draw_Curve = image.getGraphics();
}
public void paint(Graphics g){
draw_Curve.clearRect(0, 0, width, height);
draw_Curve.setColor(Color.red);
int i, j;
double x, y, r;
for(i = 0; i = 90; i++)
for(j = 0; j = 90; j++){
r = Math.PI / 45 * i * (1 - Math.sin(Math.PI / 45 * j)) * 18;
x = r * Math.cos(Math.PI / 45 * j) * Math.sin(Math.PI / 45 * i) + width / 2;
y = -r * Math.sin(Math.PI / 45 * j) + height / 4;
draw_Curve.fillOval((int) x, (int)y, 2, 2);
}
g.drawImage(image, 0, 0, this);
}
}
分析:
1.首先爱心分成两半前面三行左右一样右边就可以通过左边反相得到
中间两行是个长方形,剩下的就是个倒等腰三角形
2.在或者,长得不太像- -
System.out.println(" ** **");
for (int i = 4; i 10; i++) {
for (int a = 0; a i; a++) {
System.out.print(" ");
}
for (int b = 1; b = 2 * 9 - 2 * i - 1; b++) {
System.out.print("*");
}
System.out.print("\n");
}
** **
*********
*******
*****
***
*
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);
}
}