简体   繁体   中英

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

You could add a flag that is set to false initially, and within the if statement action code set the flag to 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.
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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