【问题标题】:Problem with Arrays that using for loops and if (JAVA)使用 for 循环和 if (JAVA) 的数组的问题
【发布时间】:2018-12-03 12:50:41
【问题描述】:

我试图弄清楚如何让 for 循环在所有数组中进行搜索(我猜问题出在名为“z”的 for 循环或 if 语句中)但它只在第一个循环中继续搜索并且然后当第一个城市不等于 cityName 时,它​​会转到 if 的 else 语句

提前致谢。

public String citiesNorthOf(String cityName)
    {
        String northOfCities = null;
        for(int z = 0 ; z < _noOfCities-1 ; z++)
        {
            if(_cities[z].getCityName().equals(cityName))
            {
                for(int a = 0 ; a < _noOfCities-1 ; a++)
                {

                    Point city1 = _cities[z].getCityCenter();
                    Point otherCity = _cities[a].getCityCenter();
                    if(city1.isUnder(otherCity))
                    {
                        northOfCities = _cities[a].getCityName();
                        System.out.println(northOfCities);
                    }
                    if(northOfCities.equals(null))
                    {
                        String noCitiesNorth = "There is no cities north of "+cityName;
                        return noCitiesNorth;
                    }
                }
            }   

            else
            {
                String noCity = "There is no city with the name: " +cityName;
                return noCity;
            }
        }

        return northOfCities;
    }

【问题讨论】:

    标签: java arrays string for-loop if-statement


    【解决方案1】:

    问题是这段代码会检查第一个城市,如果不是你要找的城市,代码会进入else块,return部分会中断循环。

    可能之后,您需要在 else 块中将标志设置为 false。 循环执行后,您检查该标志的状态。如果它是 false,那么,你执行当前在你的 else 部分中的代码。

    以下部分将出现类似问题:if(northOfCities.equals(null))。但是,我不确定这是否是您想要的行为。

    编辑: 这或多或少是我的意思:

    public String citiesNorthOf(String cityName)
        {
            String northOfCities = null;
            for(int z = 0 ; z < _noOfCities-1 ; z++)
            {
                if(_cities[z].getCityName().equals(cityName))
                {
                    for(int a = 0 ; a < _noOfCities-1 ; a++)
                    {
    
                        Point city1 = _cities[z].getCityCenter();
                        Point otherCity = _cities[a].getCityCenter();
                        if(city1.isUnder(otherCity))
                        {
                            return _cities[a].getCityName();    //If we find what we are looking for, we return the name of the city.
                        }
                        if(northOfCities.equals(null))
                        {
                            return "There is no cities north of "+cityName;   //If we find our city, but we also find that there is nothing North of it, we return this error message.
                        }
                    }
                } 
            }
            return "There is no city with the name: " +cityName;  //If the for loop has executed and none of the previous return statements have been executed, then, it follows that there is no city with the given name, so we return this error.
        }
    

    【讨论】:

    • 是的,你明白了我的意思,但你的意思是我需要输入名为“noCities”的布尔值,并检查 noCities 是真还是假,然后是 if(noCities)
    • @Daniel:抱歉耽搁了。我已经更新了我的答案。在这种情况下,我们可以使用 return 关键字,而不是使用布尔标志。在这种情况下,return 将指示逻辑流程,因为代码将根据它找到的内容到达不同的返回语句。
    【解决方案2】:

    如果你想处理数组的所有元素,你应该使用“break”而不是“return”语句,因为“return”会导致方法立即退出并返回。

    【讨论】:

      【解决方案3】:

      如果你执行到这部分代码,这将导致函数结束,因为返回:

      if(northOfCities.equals(null))
      {
          String noCitiesNorth = "There is no cities north of "+cityName;
          return noCitiesNorth;
      }
      

      这部分也是一样的:

      else
      {
          String noCity = "There is no city with the name: " +cityName;
          return noCity;
      }
      

      将return 元素替换为break 或continue 元素

      【讨论】:

        【解决方案4】:

        有几点:

        1.边界

        循环定义为for(int z = 0 ; z &lt; _noOfCities-1 ; z++)(a 相同)。 看起来最后一个花旗没有处理。如果有 10 个城市,第一个为 0,最后一个为 9。城市数减一为 9,所以当 z 为 9 时,因为 9

        2。 northOfCities作用不明确

        处于循环中并且变量的名称表明它将是城市的集合,但它是String,并且在a 变量的每个匹配循环中都会被覆盖。如果它确实被分配了一个值,它将只保存最后一个匹配的值。

        3. Null评价

        这部分代码在我看来应该在a 变量循环之外。 以这种方式,在评估了所有可能的匹配之后对其进行评估。 当循环结束时,查看变量是否仍然是null 是有意义的,这意味着它没有找到任何匹配项。 另外,如果变量是null,调用equals()会抛出NPE,所以替换成==。

                if(_cities[z].getCityName().equals(cityName))
                {
                    for(int a = 0 ; a < _noOfCities-1 ; a++)
                    {
        
                        Point city1 = _cities[z].getCityCenter();
                        Point otherCity = _cities[a].getCityCenter();
                        if(city1.isUnder(otherCity))
                        {
                            northOfCities = _cities[a].getCityName();
                            System.out.println(northOfCities);
                        }
                    }
                    if(northOfCities == null){
                            String noCitiesNorth = "There is no cities north of "+cityName;
                            return noCitiesNorth;
                    }
                }  
        

        编辑我

        通过以下更改,代码应运行打印给定城市以北的城市。该方法将返回找到的最后一个城市的名称,或者返回一条错误消息,指示该城市是否未找到,或者它是否在北部没有任何城市。我不得不假设你有一个类 Citi,如果不只是将类名替换为存储在 _cities 数组中的元素类型。

        进一步增强: 您可以更改方法以返回找到的所有城市的集合,并针对不同的错误抛出异常。

        public String citiesNorthOf(String cityName){
                String northOfCities = null; //Return value
                City citi = null;
                for(int z = 0 ; z < _noOfCities; z++){
                    if(_cities[z].getCityName().equals(cityName)){
                        citi = _cities[z];
                        for(int a = 0 ; a < _noOfCities ; a++){  
                            Point cityCenter = citi.getCityCenter();
                            Point otherCityCenter = _cities[a].getCityCenter();
                            if(cityCenter.isUnder(otherCityCenter)){
                                northOfCities = _cities[a].getCityName();
                                System.out.println(northOfCities);
                            }
                        }
                        if(northOfCities == null){
                                northOfCities = "There is no cities north of "+cityName;
                        }
                    }   
                }
                if(citi == null){
                      northOfCities = "There is no city with the name: " +cityName;
                }
                return northOfCities;
            }
        

        【讨论】:

        • 据我所知,当您想比较字符串时应该使用 equals 而不是 == ,我修复了 _noOfCities ,但仍然不知道如何解决问题。
        • 没错,但要做到这一点,您需要那里的 String 对象。如果 String 变量为 null,则没有对象,因此对该变量调用的任何方法都会抛出 NullPointerException。试试看。
        • 是的,我试过了,你能帮我解决在 else 语句处停止的 for 循环问题吗?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-14
        相关资源
        最近更新 更多