【问题标题】:Compare 2 arrays and distinguish same values in same position and same values in different positions in Java比较2个数组并区分Java中相同位置的相同值和不同位置的相同值
【发布时间】:2018-12-01 06:49:26
【问题描述】:

我碰壁了(不是字面意思)需要帮助。我对 Java 很陌生,所以需要保持这个基础才能保持我的技能水平。

我有两个包含 3 种随机颜色的 Color[] 数组。 我正在尝试识别数组中相同位置的颜色和相同但不同位置的颜色。

这对于那些熟悉 Mastermind 评分的人来说是相同的逻辑。

示例:

answerColors[]{Color.Red, Color.Blue, Color.Blue}

userColors[]{Color.Yellow,Color.Blue,Color.Red}

结果应该显示:

same color/position = 2 (this would be the Blue at [1])

same color/ different position = 1 (this would be the Red)

注意:

因为 answerColors[1] == userColors[1],请声明其位置正确,不要声明 userColors[1] 的位置不正确。

示例 2:

answerColors[]{Color.Yellow, Color.Green, Color.Blue}

userColors[]{Color.Yellow,Color.Blue,Color.Red}

结果应该显示:

same color/position = 1 (this would be the Yellow at [0]) same color/ different position = 1 (this would be the Blue)

//How will the poster distingush the difference between (Same Color, Correct Position) and (Same Color, **Different** Position)?

我看过很多回答此类问题的帖子,但没有一个帖子在比较两个数组时解决状态{相同颜色,不同位置},并阻止数组的相同部分被扫描两次。 (上面的例子有解释。#1 是最有帮助的。)

这是我的代码不起作用....结果应该是:

same color/same position = 2
same color/different position = 1

感谢您的帮助!

public void createScoreArray() {  // creates the array for     determining the values for the score panel (2 = same color/same position; 6 = not same color/same position; 1 = same color/different position ignoring those already in same color/same position
    answerColors[]{Color.Red, Color.Blue, Color.Blue};
    userColor[]{Color.Yellow, Color.Blue, Color.Red};
    int j = 0;
    scoreNumCheck = new int[3];
    for (int i = 0; i < answerColors.length; i++) {  // checking same color same position
        if (answerColors[i] == userColor[j]) {
            scoreNumCheck[i] = 2;
            System.out.println(scoreNumCheck[i]);
            j++;
         System.out.println("++++++++++++Score Pegs: " + Arrays.toString(scoreNumCheck));
        } else if (answerColors[i] != userColor[j]) {
            scoreNumCheck[i] = 6;
            j++;
        }
        System.out.println("--------- " + Arrays.toString(scoreNumCheck));


    }
    System.out.println("++++++++++++Score Pegs: " + Arrays.toString(scoreNumCheck));
    for (int s = 0; s < scoreNumCheck.length; s++) {
        if (scoreNumCheck[s] != 2) {
            for (int i = 0; i < answerColors.length; i++) {
                for (int u = 0; u < userColor.length; u++) {
                    if (userColor[u] == (answerColors[i])) {
                        scoreNumCheck[6] = 1;
                    }
                }
            }

        }
        System.out.println("2.  ++++++++++++Score Pegs: " + Arrays.toString(scoreNumCheck));
    }
}

【问题讨论】:

  • 由于您已经在循环所有答案,因此请考虑在所有用户答案中卡片不相等时再次循环并查看它们是否与当前答案匹配

标签: java arrays if-statement logic


【解决方案1】:

请尝试使用方法检查问题的措辞是否为读者所理解。 (尝试让朋友阅读。)我无法理解代码。我试图重新制作代码。

package StackOverflowAnswers;

import java.awt.Color;
import java.util.LinkedList;

public class StackOverflow53568488 {
    Color[] answerColors = {Color.RED, Color.BLUE, Color.BLUE};
    Color[] userColors = {Color.YELLOW, Color.BLUE, Color.RED}; //There is no Color.Red, there is Color.RED and Color.red.
    int [] scoreNumCheck;
    public void createScoreArray() {  
 // creates the array for determining the values for the score panel 
 //(2 = same color/same position; 
 // 6 = not same color/same position; 
 // 1 = same color/different position ignoring those already in same color/same position

        scoreNumCheck = new int[3];

        LinkedList <Integer> Results = new LinkedList<Integer>(); 
        //You should only be able to get a size of 3 in this LinkedList

        for(int i=0; i<3; i++){
            if (answerColors[i] == userColors[i]){
                Results.add(2);
            }
        }
        for(int i=0; i<3; i++){
            for(int j=0; j<3; j++){
                if(answerColors[i] == userColors[j]){ // Try drawing this stuff out...
                    Results.add(1); // add scores to Results
                }
            }
        }
        while (Results.size() < 3){
            Results.add(6); // Use the fact that you should always get a size of 3 in Results to be able to add 6 until that condition is satisfied.
        }
        //I failed to understand how you printed the answers.


        //
    }
}

【讨论】:

    猜你喜欢
    • 2015-01-30
    • 2020-08-13
    • 1970-01-01
    • 1970-01-01
    • 2016-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-18
    相关资源
    最近更新 更多