量化级方法

JPM*_*JPM 1 java math android

试图找到一种更优雅的方式来创建一个具有离散级别的int.我的例子只显示了6个不同的级别,但是我想要为45个不同的级别执行此操作,所以不要想要45个elses.不确定在数学中这叫什么,所以似乎找不到我想要的东西.

sd = some double value

int level = 0;

if (Double.compare(sd, 0.41) >= 0) {
    level = 5;
} else if(Double.compare(sd, 0.25) >= 0) {
    level = 4;
} else if(Double.compare(sd, 0.11) >= 0) {
    level = 3;
} else if(Double.compare(sd, 0.05) >= 0) {
    level = 2;
} else if(Double.compare(sd, 0.02) >= 0) {
    level = 1;
}
Run Code Online (Sandbox Code Playgroud)

................

最新更新FYI这些是我需要量化的值:你可以看到我需要一些更优雅的东西,Used Navigable Map的回答

public static final int[] Levels = { 
        3100, 3250, 3383, 3517, 3650, 
        3673, 3695, 3718, 3740, 3760, 
        3780, 3800, 3820, 3853, 3885,
        3918, 3950, 3975, 4000, 4025, 4050
        };
Run Code Online (Sandbox Code Playgroud)

Vla*_*uer 5

http://download.oracle.com/javase/6/docs/api/java/util/NavigableMap.html

NavigableMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
map.put(0, 0);    // 0..4     => 0
map.put(5, 1);    // 5..10    => 1
map.put(10, 2);   // 10..100  => 2
......
Run Code Online (Sandbox Code Playgroud)

您也可以使用Double数据类型实现它.