java - 模拟系统中的量化
问题描述
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
解决方案
似乎每个长度为 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]);
推荐阅读
- php - 如何监听 Trait 中所有可用的 Laravel 模型事件
- function - Haskell 对水平线感到困惑
- c - 如何使用C中的护士从下到上而不是从上到下画线
- c++ - 矩形上的最小切割以制成正方形
- c# - 如何实现 getBit 和 setBit 方法?
- javascript - 驯服 Visual Studio Code IntelliSense
- javascript - 在javascript中组合多个ISOStrings
- python-3.x - 为什么 sympy 解析器无法识别某些表达式是错误的?
- php - WooCommerce 订单:是否可以添加另一列并使用来自用户的自定义元字段填充它?
- bash - 代码必须在子目录中找到所有 MP4 文件才能使用 ffmpeg 进行压缩