我只是試圖查看輸入的值是否與已經在數組中的值相匹配,以及它是否返回「有效」。我知道這是很簡單的,但我不能得到這個工作:簡單數組:檢查值是否匹配
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String[] accountNums = { "5658845", "8080152", "1005231", "4520125", "4562555",
"6545231", "7895122", "5552012", "3852085", "8777541",
"5050552", "7576651", "8451277", "7881200", "1302850",
"1250255", "4581002" };
String newAccount;
String test = "Invalid";
newAccount = keyboard.next();
for (int i = 0; i < accountNums.length; i++)
{
if(newAccount == accountNums[i])
{
test = "Valid";
}
}
System.out.println(test);
}
}
謝謝你的任何援助(和耐心)
7個問題和零接受?看到這[鏈接](http://meta.stackexchange.com/a/65088/155831) – Reimeus 2013-03-04 04:05:59
http://stackoverflow.com/questions/1128723/in-java-how-can-i-test-if-an -array-contains-a-certain-value – 2013-03-04 04:11:45
考慮將'accountNums'設置爲'HashSet';檢查「HashSet」中的值是否比循環「數組」更快。 – Akavall 2013-03-04 04:21:27