【发布时间】:2012-02-14 16:58:47
【问题描述】:
我已经在这里搜索了一段时间,但一直没有找到答案。
我基本上需要使用一个数组来完成这项大学作业。然后我应该检查输入(也是一个字符串)是否与存储在字符串数组中的任何内容匹配。
我知道使用 .equals() 方法可以轻松比较字符串。但是,同样的方法不适用于 String 数组。
我为 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!");
}
}
}
如果之前有人问过这个问题,我只是错过了,我很抱歉,如果它是一个双重问题,我会删除它。
【问题讨论】:
-
您需要遍历数组并单独检查每个字符串。