如何比较数组中的某些字符?

How do I compare certain characters in arrays?

我正在尝试为学校做一个项目,我必须猜一个词。如果这个词是正确的,它会得到一个红点,如果它在某个地方,它会得到一个黄点,如果没有相似性,它会得到一个灰点。我制作了一个数组,其中包含名为数组的必须被猜测的单词,以及一个包含被猜单词的数组,每个数组条目中都有一个字符。

我认为这样的设置是可行的:

place = 0;   // The tekenaarextra and checkletters are not the problem that's just the result that happens if they are true

while(place <5){
    if(array[place] == array2[place]){
        System.out.println(array[1]);
        checkletters(place,1,Color.RED);
        tekenaarextra2();
        place ++;
    }else if( array[0] == array2[place] || array[1] == array2[place] || array[2] == array2[place] || array[3] == array2[place] || array[4] == array2[place] ){ 
        checkletters(place,1,Color.YELLOW);
        tekenaarextra2();
        place ++;
    }else{
        checkletters(place,1,Color.GRAY); // this is just a method to draw the dots.
        tekenaarextra2();
        place ++;
    }
}

但是,在使用它时我收到错误消息,而且我不太清楚如何真正比较数组中的单独条目而不将它们放入不同的字符串中,这是很多单独的工作。

比如我得到了String[] array = {a,p,p,l,e},我得到了String[] array2 = {p,l,a,t,e}

然后我想比较 array2 的第一个条目和数组的第一个条目,如果它们是相同的字符,我应该执行某个命令来绘制一个点,如果不是,否则应该将数组 2 中的第一个条目与所有其他条目进行比较,以查看它是否需要一个黄点。如果它全部测试为假并且它不包含任何它们,它应该只有一个 else 导致绘制一个灰点。这超过 5 次来比较数组 2 的所有字母,但这可以通过 while 语句简单地完成。

我需要做的事情的简化版本

if(array[0]==array2[0]){ // if the first letter of guessed word is the  first letter in the mystery word
  drawreddot();
} else if( array[0] == array2[1] || array[0] == array2[2] || array[0] == array2[3] || array[0] == array2[4]){ 
// if the letter guessed isn't in the same place but in the mystery word on another place
draw yellow dot
}else{ // if it doesn't compare to any of the entries
draw gray dot
}

如果输入数组是字符串数组,请尝试下面的代码。

private void guess(String[] guessed, String[] original){
    Set<String> set = new HashSet<>();
    for(String o : original){
        set.add(o);
    }
    for (int i = 0; i < guessed.length; i++) {
        if(guessed[i].equals(original[i])){
            System.out.println("GREEN");
        }
        else if(set.contains(guessed[i])){
            System.out.println("YELLOW");
        }
        else{
            System.out.println("GREY");
        }
    }
}

我的主要方法

public static void main(String[] args) {
    Solution solution = new Solution();
    solution.guess(new String[]{"a", "p", "p", "l", "e"}, new String[]{"p", "p", "a", "l", "x"});
}
import java.util.*;
import java.util.stream.*;
public class DotIt {
    public static void main(String[] args) {
        String mysteryWord = "apple";
        char[] mysteryChars = mysteryWord.toCharArray();
        String guessedWord = "plate";
        char[] guessedChars = guessedWord.toCharArray();
        List<Integer> mysteryCharList = mysteryWord.chars().boxed().collect(Collectors.toList());

        System.out.println("Mystery word: " + mysteryWord);
        System.out.println("Guessed word: " + guessedWord);

        for (int i = 0; i < mysteryChars.length; i++) {
            if (guessedChars[i] == mysteryChars[i]) {
                // letter of the guessed word is at same positioon in the mystery word
                System.out.println("letter " + (i + 1) + " ('" + guessedChars[i] + "') is red.");
            } else if (mysteryCharList.indexOf(Integer.valueOf(guessedChars[i])) != -1) {
                // the letter of the guessed word is in the mystery word at on another place
                System.out.println("letter " + (i + 1) + " ('" + guessedChars[i] + "') is yellow.");
            } else {
                // the letter of the guessed word isn't in the mystery word
                System.out.println("letter " + (i + 1) + " ('" + guessedChars[i] + "') is grey.");
            }
        }
    }
}
$ java DotIt.java
Mystery word: apple
Guessed word: plate
letter 1 ('p') is yellow.
letter 2 ('l') is yellow.
letter 3 ('a') is yellow.
letter 4 ('t') is grey.
letter 5 ('e') is red.
$