gpt4 book ai didi

java - 使用 MessageFormat 创建 ASCII 温度计

转载 作者:行者123 更新时间:2023-12-02 10:15:42 31 4
gpt4 key购买 nike

我正在使用Java的MessageFormat类来创建一个字符串,该字符串使用多个条件语句作为参数,使用 format方法。有了它,我使用 ASCII 字符创建了一个温度计,它接受一个随机数作为摄氏度,并根据该数字,根据条件在特定位置插入星号字符到字符串中,例如((celsius >= 25) ? "*" : " ") 。换句话说,星号代表温度计中的温度级别,具体取决于温度值。

ASCII thermometer

目前,我在 14 个参数中的每个参数中使用类似的 if 语句,然后确定是否应在所述位置 temp >= location 添加星号。或空白空间temp <= location 。我确信使用另一种方法(例如,嵌入for循环、正则表达式、函数、内置方法等

这是代码示例(注意转义字符):

// random value between -35 and 40
double celsius = Math.round(Math.random() * (40 - 35) - 35);

// ASCII Thermometer
String meter = MessageFormat.format(
" ______________________"
+ "\r\n | ^F _ ^C |"
+ "\r\n | 100 - |{0}| - 40 |"
+ "\r\n | 90 - |{1}| - 30 |"
+ "\r\n | 80 - |{2}| - 25 |"
+ "\r\n | 70 - |{3}| - 20 |"
+ "\r\n | 60 - |{4}| - 15 |"
+ "\r\n | 50 - |{5}| - 10 |"
+ "\r\n | 40 - |{6}| - 5 |"
+ "\r\n | 30 - |{7}| - 0 |"
+ "\r\n | 20 - |{8}| - -5 |"
+ "\r\n | 10 - |{9}| - -10 |"
+ "\r\n | 0 - |{10}| - -20 |"
+ "\r\n | -10 - |{11}| - -25 |"
+ "\r\n | -20 - |{12}| - -30 |"
+ "\r\n | -30 - |{13}| - -35 |"
+ "\r\n | '***` |"
+ "\r\n | (*****) |"
+ "\r\n | `---' |"
+ "\r\n |____________________|"
+ "\r\n\r\n",
((celsius >= 35) ? "*" : " "),
((celsius >= 30) ? "*" : " "),
((celsius >= 25) ? "*" : " "),
((celsius >= 20) ? "*" : " "),
((celsius >= 15) ? "*" : " "),
((celsius >= 10) ? "*" : " "),
((celsius >= 5) ? "*" : " "),
((celsius >= 0) ? "*" : " "),
((celsius >= -5) ? "*" : " "),
((celsius >= -10) ? "*" : " "),
((celsius >= -15) ? "*" : " "),
((celsius >= -20) ? "*" : " "),
((celsius >= -25) ? "*" : " "),
((celsius >= -30) ? "*" : " "));

最佳答案

你想要这个吗?

public static void main(String[] args) {
// random value between -35 and 40
double celsius = Math.round(Math.random() * (40 - 35) - 35);

final String s = " ______________________"
+ "\r\n | ^F _ ^C |"
+ "\r\n | 100 - |{0}| - 40 |"
+ "\r\n | 90 - |{1}| - 30 |"
+ "\r\n | 80 - |{2}| - 25 |"
+ "\r\n | 70 - |{3}| - 20 |"
+ "\r\n | 60 - |{4}| - 15 |"
+ "\r\n | 50 - |{5}| - 10 |"
+ "\r\n | 40 - |{6}| - 5 |"
+ "\r\n | 30 - |{7}| - 0 |"
+ "\r\n | 20 - |{8}| - -5 |"
+ "\r\n | 10 - |{9}| - -10 |"
+ "\r\n | 0 - |{10}| - -20 |"
+ "\r\n | -10 - |{11}| - -25 |"
+ "\r\n | -20 - |{12}| - -30 |"
+ "\r\n | -30 - |{13}| - -35 |"
+ "\r\n | '***` |"
+ "\r\n | (*****) |"
+ "\r\n | `---' |"
+ "\r\n |____________________|";

final int[] celsiusDegreeCompare = new int[]{
35, 30, 25, 20, 15, 10, 5, 0, -5, -10, -20, -25, -30, -35
};

final String[] parameters = new String[14];

IntStream.range(0, parameters.length).forEach(i -> {
parameters[i] = (celsius >= celsiusDegreeCompare[i]) ? "*" : " ";
});

// ASCII Thermometer
String meter = MessageFormat.format(s, parameters);

System.out.println("celsius: " + celsius);
System.out.println(meter);
}

结果:

celsius: -31.0
______________________
| ^F _ ^C |
| 100 - | | - 40 |
| 90 - | | - 30 |
| 80 - | | - 25 |
| 70 - | | - 20 |
| 60 - | | - 15 |
| 50 - | | - 10 |
| 40 - | | - 5 |
| 30 - | | - 0 |
| 20 - | | - -5 |
| 10 - | | - -10 |
| 0 - | | - -20 |
| -10 - | | - -25 |
| -20 - | | - -30 |
| -30 - |*| - -35 |
| ***` |
| (*****) |
| `--- |
|____________________|

关于java - 使用 MessageFormat 创建 ASCII 温度计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54703331/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com