java实现心型代码

空心
图示
在这里插入图片描述
代码

public class xin1 {


    public static void main(String[] args) {
        float x,y,a;
        for(y=1.3f;y>-1.1f;y-=0.06f){  
            for(x=-1.2f;x<=1.2f;x+=0.025f){
                a=x*x+y*y-1;
                System.out.print(a*a*a-x*x*y*y*y<=0.0f?' ':'s');
            }
            System.out.println();
        }
    }
}

实心
在这里插入图片描述

import static java.lang.Math.*;

public class xin2 {
    public static void main(String[] args) {
        double x,y,a;
        char s[]=new char[]{'I',' ','l','o','v','e',' ','y','o','u','!'};
        int index=0;
        for(y=1.3f;y>-1.1f;y-=0.06f){
            index=0;
            for(x=-1.1f;x<=1.1f;x+=0.025f){
                double result=x*x+pow((5.0*y/4.0-sqrt(abs(x))),2);
                if(result<=1){
                    System.out.print((s[index]));
                    index=(index+1)%11;
                }
                else{
                    System.out.print(' ');
                }
            }
            System.out.println(" ");
        }


    }
}

具体原理请请教数学老师
在这里插入图片描述


版权声明:本文为muMason原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。