[英]How do I compare a number in a certain index from two different 2d arrays?
[英]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
}
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.
$
如果输入数组是字符串数组,试试下面的代码。
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"});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.