public class loveme { private static final HashMap colorMap = new HashMap<>(){{ put(36,"qingse"); }}; public static String getColor(int color, String con){ return String.format("\33[1m\33[36m",6); } public static void main(String[] args) throws InterruptedException { int in=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 = in % str.length(); System.err.print(str.charAt(num)); in++; } else { System.err.print(" "); } } System.err.println(); Thread.sleep(100); } System.out.println(getColor(6, colorMap.get(36))); System.out.println("你像火柴一般在夜空划过"); System.out.println("刹那的烟火映照整个银河"); System.out.println("你是烟火,我是泡沫、"); }}
java爱心代码,我觉得这可以了,基础是套壳来的,做了一点修改。
改进版
import java.util.HashMap;import static javax.swing.UIManager.put;public class loveme { public enum setColor { //白色 WGITE("\33[0m", 0), //红色 RED("\33[1m\33[31m", 1), //绿色 GREEN("\33[1m\33[32m", 2), //黄色 YELLOW("\33[1m\33[33m", 3), //蓝色 BLUE("\33[1m\33[34m", 4), //粉色 PINK("\33[1m\33[35m", 5), //青色 CYAN("\33[1m\33[36m", 6); //局部变量 private String name; private int index; // 构造方法 private setColor(String name, int index) { this.name = name; this.index = index; } // 普通方法 public static String getColor(int index) { for (setColor c : setColor.values()) { if (c.getIndex() == index) { return c.name; } } return null; } // get set 方法 public String getName() { return name; } public void setName(String name) { this.name = name; } public int getIndex() { return index; } public void setIndex(int index) { this.index = index; } } public static void main(String[] args) throws InterruptedException { int in = 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 = in % str.length(); //改进版:这里更改输出颜色, System.out.print(setColor.getColor(1)); System.out.print(str.charAt(num)); in++; } else { System.out.print(" "); } } System.err.println(); Thread.sleep(100); } //这里更改文案颜色输出字段 System.out.println(setColor.getColor(6) + "测试青色文字"); System.out.println("你像火柴一般在夜空划过"); System.out.println("刹那的烟火映照整个银河"); System.out.println("你是烟火,我是泡沫、"); }}
改进版可以让爱心颜色也改变,我感觉还是很好的
更改爱心颜色在这一行
效果图
来源地址:https://blog.csdn.net/weixin_75190682/article/details/128860039