eclipse - Java deck of cards -
i'm trying piece of java return int total of value of cards in deck. using test data of 10 cards of different values should result 64 i'm getting random answers around 73 every time run it, changes slightly. got ideas why?? confused piece of program trying return array of cards of suit(in test case hearts). if code messy or wrong because i'm beginner haha. methods , stuff have been completed lecturer im trying show me in right direction methods.
thanks in advance.
1st question
public int totalpack() { int total = 0; ( int = 0; < pack.size(); i++){ total = total + pack.get(i).getnumber() ; } return total; }
end
2nd question
public arraylist<card> findsuit(string suit) { ( int = 0; < pack.size(); i++){ if (pack.get(i).getsuit().equals(suit)){ return null; } else return ???; } return findsuit(suit); }
for second question:
as jurfer mentions, don't need recursion. need follow 4 simple steps:
- create new arraylist
- iterate on cards
- check whether each card of desired suit, , if add arraylist
- return arraylist.
check code:
public arraylist<card> findsuit(string suit) { // 1. create new arraylist arraylist<card> list = new arraylist<>(); // 2. iterate on cards ( int = 0; < pack.size(); i++){ // 3. if card of desired suit if (pack.get(i).getsuit().equals(suit)){ // add list list.add(pack.get(i)); } // 4. return arraylist return list; }
Comments
Post a Comment