如何在“if”陳述句為真后重新設定“city”物件并從頭開始重新啟動“for”陳述句?
private SortedSet<City> cityList;
for(City city:this.cityList)
{
if(city.getPopulation()==arr[i])
{
System.out.printf(city.getName() "(" city.getCountry() ") population: " city.getPopulation() " area: " city.getArea());
i ;
}
}
uj5u.com熱心網友回復:
這是你想要完成的嗎?
import java.util.Set;
public class City {
public static void main(String[] args) {
Integer[] arr = { 50_000, 100_000, 200_000 };
Set<City> cityList = Set.of( //
new City(100_000, "city1", "country1", "area1"), //
new City(200_000, "city2", "country2", "area2"), //
new City(350_000, "city3", "country3", "area3") //
);
outer: for (int i = 0; i < arr.length; i ) {
for (City city : cityList) {
if (city.getPopulation() == arr[i]) {
System.out.println(city.getName() "(" city.getCountry() ") population: "
city.getPopulation() " area: " city.getArea());
continue outer;
}
}
}
}
private int population;
private String name;
private String country;
private String area;
public City(int population, String name, String country, String area) {
this.population = population;
this.name = name;
this.country = country;
this.area = area;
}
public int getPopulation() {
return population;
}
public void setPopulation(int population) {
this.population = population;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
}
嵌套回圈時,您可以為它們添加標簽(https://www.tutorialspoint.com/break-continue-and-label-in-Java-loop)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/377916.html
上一篇:在ReactNative中使用react-navigationv6的身份驗證流程問題。錯誤:無效的掛鉤呼叫。不變違規:“主要”尚未注冊
下一篇:在Java泛型中指定Map
