나는 hashMaps에 관해 뭔가를 보았지만, 우리는 그 일을 멀리하지 않았다. 답변과 도움이되는 제안은 관리 할 수있는만큼 간단하게하십시오.목록에서 사용자 지정 데이터 형식의 값을 계산하는 방법은 무엇입니까?
나는 이미 만들어진 Color라는 완벽하게 작동하는 사용자 지정 데이터 형식이 있습니다. 유형의 유일한 값은 Color.BLUE, Color.RED, Color.YELLOW 및 Color.GREEN입니다.
내 작업은 다른 색상보다 목록에 더 많은 Color.BLUE가있는 경우 Color.BLUE를 반환하고 다른 색상보다 목록에 더 많은 Color.RED가 있으면 Color.RED를 반환하려면 Color와 동일합니다. 녹색과 컬러. 노란색. 내가 연구하고하고있다
이 코드를 내놓았다 :
public Color callColor(List<Card> hand) {
int blueCards = Collections.frequency(hand, Color.BLUE);
int redCards = Collections.frequency(hand, Color.RED);
int greenCards = Collections.frequency(hand, Color.GREEN);
int yellowCards = Collections.frequency(hand, Color.YELLOW);
Color changeColorTo = Color.NONE;
if ((blueCards > redCards) || (blueCards > greenCards) || (blueCards > yellowCards)) {
changeColorTo = Color.BLUE;
}
if ((redCards > blueCards) || (redCards > greenCards) || (redCards > yellowCards)) {
changeColorTo = Color.RED;
}
if ((greenCards > redCards) || (greenCards > blueCards) || (greenCards > yellowCards)) {
changeColorTo = Color.GREEN;
}
if ((yellowCards > redCards) || (yellowCards > greenCards) || (yellowCards > blueCards)) {
changeColorTo = Color.YELLOW;
}
return changeColorTo;
}
하지만 그들은 확실히 제로이어야한다 때이 코드는 0으로 blueCards, redCards, greenCards 및 yellowCards 모든됩니다.
그래서이 경우 내 Collections
구현이 전혀 작동하지 않았습니다. 도움!
에 대한 카드 수를 알아내는 반복적 인 방법은
다음은? – Sanjeev
@Sanjeev 확실하지 않습니다. 코드의 해당 부분이 우리에게 제공되었습니다. – hattic
'Color' 클래스의 코드를 게시 할 수 있습니까? – Sanjeev