簡體   English   中英

使用for循環和if(JAVA)的數組出現問題

[英]Problem with Arrays that using for loops and if (JAVA)

我試圖找出如何在所有數組內搜索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;
    }

問題在於此代碼將檢查第一個城市,如果它不是您要查找的城市,則該代碼將進入else塊,並且return部分會中斷循環。

可能需要做的是,需要在else塊中將標志設置為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.
    }

如果要處理數組的所有元素,應該使用“ break”而不是“ return”語句,因為“ return”將導致該方法立即退出並返回。

如果執行到代碼的這一部分,則由於返回,這將導致函數結束:

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;
}

breakcontinue元素替換return元素

幾件事情:

1.邊界

循環定義為for(int z = 0 ; z < _noOfCities-1 ; z++) (與a相同)。 看起來最后一個花旗未處理。 如果有10個城市,並且第一個城市為0,則最后一個城市為9。城市數減去1將為9,因此當z為9時,因為9 <9為假,將不進行處理。

2. northOfCities角色不清楚

作為在一個循環和變量的名稱表明,它是城市的集合,而是它是一個String ,並得到在每個匹配循環覆蓋a變量。 如果實際分配了一個值,它將只保留最后一個匹配的值。

3. Null評估

在這部分代碼在我看來,應該是外面a可變環。 這樣,在所有可能的匹配都已被評估后,將對其進行評估。 循環結束后,有必要查看變量是否仍然為null ,這意味着它沒有找到任何匹配項。 另外,如果變量為null ,則調用equals()會拋出NPE ,因此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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM