Results 1 to 1 of 1
- 05-08-2012, 07:35 PM #1
Member
- Join Date
- May 2012
- Posts
- 4
- Rep Power
- 0
ternary in the condition of a for loop
Hi,
I have a method
It is used to split any amount of cards into the specified amount of "piles". I have it set up so it is scalable simply having the array "deckOfCards" to contain cards from multiple decks and then changing the pilesNeeded variable will still split them up into as close to equal sized values as possible. I also have a couple of statements for swing should be able to be ignored.Java Code:/* * Calculate the number of cards needed per "pile" and then add the * PlayingCard from deckOfCards to the cardPiles object */ int baseCardsPerPile = deckOfCards.length / pilesNeeded; int remainderForPiles = deckOfCards.length % pilesNeeded; for (int i = 0, j = 0; i < pilesNeeded; i++) { cardPiles[i] = new CardCollection(deckOfCards[j]); for (int k = 0; k < baseCardsPerPile + (i < remainderForPiles ? 1 : 0); k++, j++) { cardPiles[i].addToBottom(deckOfCards[j]); } cardPiles[i].setBounds(xpos[i], ypos[i], cardPiles[i].getIcon() .getIconWidth(), cardPiles[i].getIcon().getIconHeight()); add(cardPiles[i]); }
However, the ternary operator I have in the for condition seems messy to me. It is simply used to distribute the remainder of cards after division if there is a remainder.
How can I do this without having a ternary in the condition? Would I somehow nest the inner for loop in an if condition?
Thanks,
Mike
Similar Threads
-
Question about ternary operators.
By Harrison in forum New To JavaReplies: 6Last Post: 11-12-2011, 07:54 AM -
Shorthand ternary operator ?
By Billywizz in forum New To JavaReplies: 3Last Post: 10-02-2011, 09:57 PM -
doubt regarding ternary operator (?)
By subith86 in forum New To JavaReplies: 2Last Post: 03-10-2011, 08:15 AM -
While loop condition
By counterfox in forum New To JavaReplies: 3Last Post: 10-10-2010, 01:14 AM -
Use of ternary
By wulfgarpro in forum New To JavaReplies: 1Last Post: 09-21-2010, 06:31 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks