2017-12-03 12 views
0

플레이어가 취할 수있는 동작 중 하나가 스왑 옵션 인 카드 게임을 만들려고합니다.이 옵션은 재생중인 카드 (ArrayList의 index0에있는 요소)를 가져 와서 다른 카드로 교체합니다. 그 다른 카드의 색인은 해당 카드의 health * power를 계산하는 다른 클래스의 메소드를 호출하는 private 메소드를 통해 발견됩니다 (그리고 determinePower로 레이블 됨). 가장 높은 결정력을 가진 카드가 index0의 카드와 교환됩니다.해당 요소의 인덱스가 개인 메서드를 통해 발견되면 ArrayList의 index0에있는 요소를 다른 요소로 바꿀 수 있습니까? (Java)

지금까지 필자가 작성한 것은 메소드가 같은 유형이 아니기 때문에 오류가 있습니다. 이 기능이 작동하려면 어떻게해야합니까?

public java.lang.String swap() 
{ 
    if (playerHand.size()>1) //playerHand is the ArrayList<Card> here 
    { 
     return "-1"; //if the hand only has one card, it cannot swap with anything and must return -1 
    } 
    else 
    { 
     Card temp = playerHand.get(0); //card is an object 
     playerHand.set(0,playerHand.get(findCard())); //this is where the error is 
     playerHand.set(findCard(),temp); //error also here 
    } 
} 

이것은 메소드 swap()에서 참조되는 개인 메소드 findCard()입니다.

private java.lang.String findCard() //a method of swap that searches for the index number of the element in the array with the highest value 
{ 
    int largest = 0; 
    for (int i=1; i<playerHand.size(); i++) 
    { 
     if (playerHand.get(i).determiningPower()>playerHand.get(largest).determiningPower()) 
     { 
      largest = i; //determiningPower is a method in a separate class that calculates the health*power. this should find which card has the highest determiningPower 
     } 
    } 
    return java.lang.String.valueOf(largest); 
} 

저는 매우 새로운 것이므로 어떤 종류의 도움이라도 대단히 감사하겠습니다!

+0

if 문이 잘못되었습니다. 목록에 요소가 있는지 확인하고 요소가 없는지 확인하려고합니다. 그래서 '(playerhand.size()> 1)'을'playerhand.size() <1'로 변경하십시오. – Harsh

+0

@ 그것을 지적 해 주셔서 고맙습니다! 오류는 실제로 else 문에서 발생합니다. 특히 ** .get ** 및 **. set **. 그것은 "적절한 메소드가 없습니다"라고 말했습니다. – user8638151

+0

'findCard' 메소드는'int'를 리턴해야하지만'String' 값을 가져야한다고 생각합니다. –

답변

0

set 메서드를 사용하여 값을 설정하지 마십시오. 사실 목록에는 그런 방법이 없으므로 오류는 no suitable method found입니다.

목록은 동적이며 값을 설정하는 데 실제로 n 개의 인덱스가 없습니다. 필요한 것은 가치를 추가하는 것입니다. 대신 playerHand.add(playerHand.get(findCard()));을 사용해보세요.

또한 get은 목록에서 값을 제거하지 않습니다. get, addremove 기능을 적절히 사용해 보시기 바랍니다.