我在这里搜索了一段时间,但还没有找到答案。
我的大学作业要求我使用一个数组。然后,我应该检查输入的字符串是否与存储在字符串数组中的任何内容匹配。
我知道可以使用.equals() 方法轻松比较字符串。然而,同样的方法不能用于字符串数组。
为了在StackOverflow上解释,请参考下面的示例代码。
我做错了什么?
import java.util.Scanner;
class IdiocyCentral {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
/*Prints out the welcome message at the top of the screen*/
System.out.printf("%55s", "**WELCOME TO IDIOCY CENTRAL**\n");
System.out.printf("%55s", "=================================\n");
String [] codes = {"G22", "K13", "I30", "S20"};
System.out.printf("%5s%5s%5s%5s\n", codes[0], codes[1], codes[2], codes[3]);
System.out.printf("Enter one of the above!\n");
String usercode = in.nextLine();
if (codes.equals(usercode)) {
System.out.printf("What's the matter with you?\n");
}
else {
System.out.printf("Youda man!");
}
}
}
如果这个问题之前已经被问过而我错过了,那么我很抱歉。如果这是一个重复的问题,我会将其删除。