首页 > 解决方案 > 模拟系统中的量化

问题描述

1.25的值,如何在我创建的数组中获取正确的值并在屏幕上打印这个值?

static final String[] DIGITS = {
    "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111"
};

for example 0.00 - 1.25 => 0000, 1.25 - 2.50=> 0001 8.75-10.0 => 0111

标签: java

解决方案


似乎每个长度为 1.25 的间隔都映射到数组的一个元素。然后,您可以通过将提供的值除以 1.25 来计算指数。

int index = (int)(value / 1.25);
if (index < 0) {
    index = 0;
}
if (index > DIGITS.length - 1) {
    index = DIGITS.length - 1;
}
System.out.println(value + " => " + DIGITS[index]);

推荐阅读