简体   繁体   English

如何检查是否从未使用过for循环内的if语句?

[英]how to check if an if statement inside of a for loop is never used?

i created a Method to check if users location are close to a building. 我创建了一个方法来检查用户位置是否靠近建筑物。

Therefore i created the following method 因此,我创建了以下方法

private void whereAreYou(final List<location> locations) {
    loadingprogress.setVisibility(View.INVISIBLE);

    for (int i = 0; i < this.cities.size(); i++) {
        final location loc= locations.get(i);
        Location loc1 = mLastLocation;
        Location loc2 = new Location("");
        loc2.setLatitude(loc.getLat());
        loc2.setLongitude(loc.getLng());

        float distanceInMeters = loc1.distanceTo(loc2);
        int distanceInKm = (int) (distanceInMeters / 1000);

        if (distanceInKm <= Integer.parseInt(currentclub.getRadius())) {
            uploadInformationToUserProfile(loc.getName());
        }
    }
}

Now I have to check if a value was within the if statements or not, because then another method must be called, but only if all values ​​do not fit 现在我必须检查一个值是否在if语句内,因为这时必须调用另一个方法,但前提是所有值都不适合

You could add a flag that is set to false initially, and within the if statement action code set the flag to true. 您可以添加最初设置为false的标志,并在if语句操作代码内将标志设置为true。 Following that section of code print it to the console. 在该部分代码之后,将其打印到控制台。

Then iterate through the appropriate test cases and check if it ever appears as true. 然后遍历适当的测试用例,并检查其是否真实存在。

Add a boolean flag. 添加一个布尔标志。

private void whereAreYou(final List<location> locations) {
    loadingprogress.setVisibility(View.INVISIBLE);

    boolean locationMatchFound = false; //boolean flag

    for (int i = 0; i < this.cities.size(); i++) {
        final location loc= locations.get(i);
        Location loc1 = mLastLocation;
        Location loc2 = new Location("");
        loc2.setLatitude(loc.getLat());
        loc2.setLongitude(loc.getLng());

        float distanceInMeters = loc1.distanceTo(loc2);
        int distanceInKm = (int) (distanceInMeters / 1000);
        if (distanceInKm <= Integer.parseInt(currentclub.getRadius())) {
            locationMatchFound = true; //boolean flag set to true if location found

            uploadInformationToUserProfile(loc.getName());
        }
    }
    if (!locationMatchFound){
        //Call your other method.
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM