【发布时间】: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