Java编写简单的爱心

  • Post author:
  • Post category:java


多的不说,直接先给大家看一眼效果啊!!!



简约版

在这里插入图片描述

其实很简单,就用到了一个嵌套for循环实现。需要代码的自取哦!!

public class love {
    public static void main(String[] args) {
        for (float y=1.5f;y>-1.5f;y-=0.15f){
            for (float x=-1.5f;x<1.5f;x+=0.05){
                float a = x*x+y*y-1;
                if ((a*a*a-x*x*y*y*y)<=0.0){
                    System.out.print("*");
                }else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }  
}

搞完以后觉得太单调了,就用*拼凑出来,没有那种浪漫的感觉,就换了字符串来实现。然后就这样啦



升级版

在这里插入图片描述

现在看起来更不错一点,注意哦!!我们的字符是可以改变的。。

public class iloveyou {
    public static void main(String[] args) throws InterruptedException {
        int count=0;
        for (float y=2.5f;y>-2.0f;y-=0.12f){
            for (float x=-2.3f;x<2.3f;x+=0.041f){
                float a = x*x+y*y-4f;
                if ((a*a*a-x*x*y*y*y)<-0.0f){
                    String str="I LOVE YOU!";
                    int num =count%str.length();
                    System.err.print(str.charAt(num));
                    count++;
                }else{
                    System.err.print(" ");
                }
            }
            System.err.println();
            Thread.sleep(100);
        }
    }
}

代码很简洁,很值得看看啦!!



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