Java中,如何与字符串数组比较字符串 [英] Java, how to compare Strings with String Arrays
问题描述
我一直在寻找在这里有一段时间了,但一直没能找到它的答案。
I have been searching here for some time but haven't been able to find the answer to it.
我基本上需要使用数组从大学这个任务。然后,我应该检查输入(这也是一个String)匹配任何的存储字符串数组中。
I am basically required to use an array for this assignment from college. And then I am supposed to check that the input (which is also a String) matches whatever's stored within the String array.
我知道有一个可以很容易地通过使用.equals()方法比较字符串。然而,同样的方法不与字符串数组工作
I know one can easily compare Strings by using the .equals() method. However, the same method is not working with the String array.
我创建code以下示例计算器的目的,所以你可以用它来解释给我,如果你愿意的话。
I created the following example of code for the purpose of StackOverflow so you can use it to explain it to me, if you'd like.
我在做什么错了?
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!");
}
}
}
我道歉,如果这已经被问过,我只是错过了它,如果它的双重问题,我会删除它。
I apologize if this has been asked before and I just missed it, if its a double question, I will remove it.
推荐答案
I $你是想检查数组包含一定的价值,是p $ psume?如果是这样,使用包含
方法。
I presume you are wanting to check if the array contains a certain value, yes? If so, use the contains
method.
if(Arrays.asList(codes).contains(userCode))
这篇关于Java中,如何与字符串数组比较字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!