0

I found this code online that helps me with my homework, but I don't understand what it means. What I want to do is create a method called "handScore" that adds together the ranks of a card array. The value of each card is basically what their rank is, However, if the rank of the card is a jack, queen, or king, the value will simply be 10 and ace has a value of 1.

This is the code I found

public static int handScore (Cards[] cards){
    int handTotal = 0; 
    for(Cards c : cards) {  
        int cardTotal = c.rank;  

        if(cardTotal > 10){ 
            cardTotal = 10; 
        } 
        handTotal += cardTotal; 
    }
    return handTotal;
}

My main confusion is about the colon in line 3, what does that do?

Henrik Aasted Sørensen
  • 6,966
  • 11
  • 51
  • 60
  • It is part of the **Java syntax** and means *"for each `c` in `cards`"*. On the right side you can write everything which is of type `Iterable`. The syntax is equivalent to fetching an `Iterator` by `cards.iterator()` and then doing `while(iter.hasNext()) { Cards c = iter.next(); ... }`. – Zabuzard Nov 25 '17 at 11:51

1 Answers1

1

You can translate it as "For each Cards object in cards array do the following actions {...}"

Przemysław Moskal
  • 3,551
  • 2
  • 12
  • 21