我正在嘗試為學校制作一個專案,我必須猜一個詞。如果單詞是正確的,它會得到一個紅點,如果它在某個地方,它將得到一個黃點,如果沒有相似性,它將得到一個灰點。我已經創建了一個陣列,其中包含必須被猜測的名為陣列的單詞,以及一個包含被猜測單詞的陣列,每個陣列條目中都有一個字符。
我認為像這樣的一些設定是可行的:
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
}
uj5u.com熱心網友回復:
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.
$
uj5u.com熱心網友回復:
如果輸入陣列是字串陣列,試試下面的代碼。
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"});
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/481911.html
上一篇:為什么Spring/java不支持Optional和@PathVariable組合?
下一篇:以陣列為元素的Java3D陣列
