我已經創建了一個類,其目標是擁有一個方法,在該類中接收一個字符串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