我已经创建了一个类,其目标是拥有一个方法,在该类中接收一个字符串x,并且它的行程通过循环查看接收到的字符串是否与String []中的任何字符串匹配。这是我的代码:输入一个字符串,看看字符串是否与数组中的任何字符串匹配(JAVA)
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);
}
}
不应该这样做吗?当我输出布尔发现,它不断给我假的... ...
仍然不起作用:S – 2012-03-24 19:48:57
对不起,它确实有效。非常感谢你,它是有道理的,它不起作用 – 2012-03-24 19:51:06
很酷,很高兴帮助! – 2012-03-24 19:51:54