Reputation: 51
I need to find the sum of the ranks from 13 cards that a user is dealt.
this is in my class card.java which returns the value of the card as a string then the suit as a string and then the rank as an int.
public String toString(){
String s = ("I am the " + rankString[rank-2] + " of " + suit + "(rank): " + rank);
return s;
}
this is in my CardFrame class. CardFrame.java
private void doButton1Stuff(){
//textArea.append("\nButton 1 Clicked");
textArea.setText("");
for(int i = 0; i<13; i++){
textArea.append(deck.getNextCard().toString()+"\n");
}
everything is working fine i just need to figure out a way to add all the ranks together from the 13 cards the user is dealt and output that as their score.
public Card getNextCard(){
//check for end of deck
topCard++;
if(topCard==53){
topCard = 1;
shuffle();
}
return cards[deck[topCard-1]];
Upvotes: 0
Views: 112
Reputation: 1
It looks like your "Card" class (I can't tell what you've named it from your code) has a "rank" member which I'm guess is an integer. So I think what you're having trouble with is the iteration. Try this:
private void doButton1Stuff(){
//textArea.append("\nButton 1 Clicked");
textArea.setText("");
int score = 0;
for(int i = 0; i<13; i++){
Card card = deck.getNextCard();
score += card.rank
textArea.append(card.toString()+"\n");
}
textArea.append("Score: ");
textArea.append(score);
}
This assumes that your member var "rank" is accessible (see "public", "protected", "private")
Upvotes: 0
Reputation: 124225
In case you cant access to your rank value from Card class you can always read it from its toString()
output like this:
String cardToStringOutput = "I am the " + "rankString[rank-2]" + " of "
+ "suit" + "(rank): " + "11";
int rank=Integer.parseInt(cardToStringOutput.split(" ")[6]);
System.out.println("rank="+rank);
But consider to add some getter to Card class so you don't have to do use above code.
Upvotes: 0
Reputation: 106430
You'd need an accessor for your rank
variable.
public int getRank() {
return rank;
}
Then you can use it for every card in some collection (here I assume an array):
for(Card c: cards) {
sum += c.getRank();
}
For your use case, it could be used here:
int sum = 0;
for(int i = 0; i<13; i++) {
Card c = deck.getNextCard();
textArea.append(c.toString()+"\n");
sum += c.getRank();
}
Upvotes: 2