Java 询问包含数组中包含的字母的单词

Java 询问包含数组中包含的字母的单词,java,arrays,Java,Arrays,我有个问题。我希望我的程序要求我插入一个单词,这个单词的字母必须包含在一个数组中 目前,我想知道数组中是否包含字母,print(“字母正确”),else print(“字母不正确”) 但总是返回(“信不正确”),为什么 请帮帮我 公共类UTIL{ Scanner ent = new Scanner(System.in); String insertedLetter = ""; String letters[] = {"a", "b", "c", "d", "e", "f", "g", "i",

我有个问题。我希望我的程序要求我插入一个单词,这个单词的字母必须包含在一个数组中

目前,我想知道数组中是否包含字母,print(“字母正确”),else print(“字母不正确”)

但总是返回(“信不正确”),为什么

请帮帮我

公共类UTIL{

Scanner ent = new Scanner(System.in);
String insertedLetter = "";
String letters[] = {"a", "b", "c", "d", "e", "f", "g", "i", "j", "l", "m", "n", "o", "p", "r"};
String saveLetter = "";

public void askLetter() {
    for (int i = 0; i < letters.length; i++) {
        saveLetter += " " + letters[i] + ",";
    }
    System.out.println("Insert a word that contains these letters " + saveLetter);
    insertedLetter = ent.nextLine();

    if (saveLetter.equals(insertedLetter)) {
        System.out.println("The letter is correct");
    } else {
        System.out.println("The letter is incorrect");
    }
}
Scanner ent=新扫描仪(System.in);
字符串插入器=”;
字符串字母[]={“a”、“b”、“c”、“d”、“e”、“f”、“g”、“i”、“j”、“l”、“m”、“n”、“o”、“p”、“r”};
字符串saveLetter=“”;
公开无效askLetter(){
for(int i=0;i

}

一种简单的方法如下:

    boolean contained = true;
    for (int i = 0; i < insertedLetter.length(); i++) {
        if (saveLetter.indexOf(insertedLetter.charAt(i)) == -1) {
            contained = false;
            break;
        }
    }
    if (contained) {
        System.out.println("The letters are all contained in the saveLetter array");
    } else {
        System.out.println("One or more of the input letters are not contained in the saveLetter array");
    }
boolean contained=true;
对于(int i=0;i
您认为
方法等于
的作用是什么?只有当输入为“a、b、c、d、e、f、g、i、j、l、m、n、o、p、r”时,您的程序才会正确输出。我不确定这就是你要找的。有一个,而且只有一个,输入与你的
saveLetter
-即插入一个包含这些字母a、b、c、d、e、f、g、I、j、l、m、n、o、p、r、a、b、c、d、f、g、I、j、m、n、l、l、m、n、o、p、r的单词,字母是正确的“a,b,c,d,e,f,g,i,j,l,m,n,o,p,r,”…您需要尝试一个“contains”方法。Is
insertedLetter.matches(“[a-r]+”)
做什么?不确定是否接受空字符串-这将在单独的检查中完成。我希望插入“f”,返回“单词是正确的”,因为字母“f”“包含在数组中。我得到的印象是,他想一个字母一个字母地打印,但您的代码可以很容易地进行调整。哦!非常感谢你!这就是我想要的!非常感谢你!