Get the number closest to the target value in a set of data based on the target value

   public static double getWind(double x) {
    
    
        double[] arr = {
    
    0.0, 22.5, 45, 67.5, 90, 112.5, 135, 167.5, 180, 202.5, 225, 247.5, 270, 292.5, 315, 337.5};
        double minDifference = Math.abs(arr[0] - x);
        int minIndex = 0;
        for (int i = 1; i < arr.length; i++) {
    
    
            double temp = Math.abs(arr[i] - x);
            if (temp < minDifference) {
    
    
                minIndex = i;
                minDifference = temp;
            }
        }
        return arr[minIndex];
    }

Guess you like

Origin blog.csdn.net/qq_45752401/article/details/125104972
Recommended