我只是想看看输入的值是否匹配数组中已经存在的值,并且是否确实返回“有效”。我意识到这很简单,但是无法正常工作:

 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);
    }
}


感谢您的帮助(和耐心等待)

最佳答案

使用equals方法。检查here原因。

if (newAccount.equals(accountNums[i]))

09-12 10:33