首页 > 解决方案 > 获取方法被调用的问题

问题描述

我似乎无法得到坐标以上和百分比以上来输出任何内容。我有一个 List<> coordinatesAbove 方法,它采用高度并将纬度和经度放入列表中。然后,我需要计算出我在新列表中拥有的所有坐标与旧列表相比的百分比。我下面有这个。每当我运行它时,我似乎无法获得要输入的百分比。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.List;

public class Map {

static double[][] array;
double longitude, latitude;
double alti;

public static void main(String[] args) throws FileNotFoundException {
    Map m = new Map();
    m.readDataArray("earth.xyz");
    System.out.println(Arrays.deepToString(array));
    m.coordinatesAbove(-2000);
    m.percentageAbove(-2000);
    System.out.println(percentage);
}

public void readDataArray(String filename) throws FileNotFoundException {
    Scanner input = new Scanner(new File(filename));
    int countLines = 0;
    while (input.hasNextLine()) {
        countLines++;
        input.nextLine();
    }
    array = new double[2336041][3];
    input.close();
    input = new Scanner(new File(filename));
    String curLine;
    double longitude, latitude;
    double alti;
    countLines = 0;
    while (input.hasNextLine()) {
        curLine = input.nextLine();
        String[] curData = curLine.split("\t");
        longitude = Double.parseDouble(curData[0]);
        latitude = Double.parseDouble(curData[1]);
        alti = Double.parseDouble(curData[2]);
        array[countLines][0] = longitude;
        array[countLines][1] = latitude;
        array[countLines][2] = alti;
        countLines++;
    }
}

public List<Double> coordinatesAbove(double altitude){
    List<Double> coordinatesAbove = new ArrayList<>();
    for (int i = 0; i < array.length; i++) {
        coordinatesAbove.add(array[i][2]);
    }
    coordinatesAbove.removeIf(a -> a < altitude);
    return coordinatesAbove;
}

public double percentageAbove(double altitude) {
    double percentage = 0;
    for (int i = 0; i < array.length; i++) {
        if (coordinatesAbove(-2000).contains(array[i][2]))
            percentage = percentage + 100 / array.length;
    }
    return percentage;
}
}

标签: javaarrays

解决方案


您的方法返回结果,但您没有将其分配到任何地方。

m.percentageAbove(-2000);

应该

double percentage = m.percentageAbove(-2000);

推荐阅读