【发布时间】:2012-04-08 22:36:01
【问题描述】:
我创建了一个类,其目标是在该类中接收一个字符串 x,然后它通过一个循环来查看接收到的字符串是否与 String[] 中的任何字符串匹配。这是我的代码:
public class MatchCountry
{
public boolean findCountry(String a)
{
boolean match = false;
String [] euCountries = {"Albania", "Andorra", "Armenia", "Austria", "Azerbaijan", "Belarus", "Belgium", "Bosnia and Herzegovina",
"Bulgaria", "Croatia", "Cyprus", "Czech Republic", "Denmark", "Estonia", "Finland", "France", "Georgia", "Germany", "Greece",
"Holland", "Iceland", "Ireland", "Italy", "Latvia", "Liechtenstein", "Lithuania", "Luxembourg", "Macedonia", "Malta",
"Moldova", "Monaco", "Montenegro", "Netherlands", "Norway", "Poland", "Portugal", "Romania","Russia","San Marino",
"Serbia", "Slovakia", "Slovenia", "Spain", "Sweden", "Switzerland", "Turkey", "Ukraine", "United Kingdom", "Vatican City"};
int l = euCountries.length;
for (int i = 0; i < l; i++)
{
System.out.println(euCountries[i]);
if (a == euCountries[i])
match = true;
else
match = false;
}
return match;
}
public static void main (String args[])
{
MatchCountry mc = new MatchCountry();
boolean found = mc.findCountry("Portugal");
System.out.println(found);
}
}
这不应该吗?当我输出找到的布尔值时,它一直给我 FALSE ...
【问题讨论】:
标签: java arrays string for-loop boolean