猿问

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

我创建了一个方法来检查用户位置是否靠近建筑物。


因此,我创建了以下方法


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());

        }

    }

}

现在我必须检查一个值是否在if语句内,因为那必须调用另一个方法,但前提是所有值都不适合


MMTTMM
浏览 198回答 2
2回答

一只斗牛犬

您可以添加最初设置为false的标志,并在if语句操作代码内将标志设置为true。在该部分代码之后,将其打印到控制台。然后遍历适当的测试用例,并检查其是否真实存在。

慕斯709654

添加一个布尔标志。private void whereAreYou(final List<location> locations) {&nbsp; &nbsp; loadingprogress.setVisibility(View.INVISIBLE);&nbsp; &nbsp; boolean locationMatchFound = false; //boolean flag&nbsp; &nbsp; for (int i = 0; i < this.cities.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; final location loc= locations.get(i);&nbsp; &nbsp; &nbsp; &nbsp; Location loc1 = mLastLocation;&nbsp; &nbsp; &nbsp; &nbsp; Location loc2 = new Location("");&nbsp; &nbsp; &nbsp; &nbsp; loc2.setLatitude(loc.getLat());&nbsp; &nbsp; &nbsp; &nbsp; loc2.setLongitude(loc.getLng());&nbsp; &nbsp; &nbsp; &nbsp; float distanceInMeters = loc1.distanceTo(loc2);&nbsp; &nbsp; &nbsp; &nbsp; int distanceInKm = (int) (distanceInMeters / 1000);&nbsp; &nbsp; &nbsp; &nbsp; if (distanceInKm <= Integer.parseInt(currentclub.getRadius())) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; locationMatchFound = true; //boolean flag set to true if location found&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uploadInformationToUserProfile(loc.getName());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if (!locationMatchFound){&nbsp; &nbsp; &nbsp; &nbsp; //Call your other method.&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答