gpt4 book ai didi

java - if 语句 OR 逻辑奇数

转载 作者:太空狗 更新时间:2023-10-29 14:29:10 25 4
gpt4 key购买 nike

我使用以下信息从 jUnit 测试用例调用此函数:

// abbr = "US";
// Countries = array of two objects one with iso2 = "us"
public Country getCountryFromAbbr(String abbr) {
abbr = abbr.toLowerCase();
for (int i = 0; i < Countries.size(); i++) {
Country country = Countries.get(i);
String iso2 = country.ISO2.toLowerCase();
String iso3 = country.ISO3.toLowerCase();

if (iso2.equals(abbr) || iso3.equals(abbr)) {
return country;
}
}

return null;
}

当我调试时,us iso2.equals(abbr) ISO2 的第二个对象为 true,另一个为 错误。但是,国家/地区未返回,调试器完成循环并返回 null

我一头雾水 ||假的是真的。我错过了什么吗?

这是国家的模拟:

    List<Country> Countries = new ArrayList<Country>();
Country country = new Country();
country.CountryId = 1;
country.CountryName = "Great Britian";
country.ISO2 = "GB";
country.ISO3 = "GBR";
Countries.add(country);

Country usa = new Country();
usa.CountryId = Studio.USA_COUNTRY_ID;
usa.CountryName = "United States of America";
usa.ISO2 = "US";
usa.ISO3 = "USA";
Countries.add(usa);
return Countries;


编辑:我正在使用 Eclipse 并使用我的 Droid X 2.3.3 进行调试

最佳答案

它是否适用于简单的 if 条件?

if (iso2.equals(abbr)) {
return country;
}
if(iso3.equals(abbr)){
return country;
}

关于java - if 语句 OR 逻辑奇数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8499418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com