【发布时间】: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