text:
Results 1 to 9 of 9
Thread: List of chars
List of chars
So im making a card game application for computer science and my question is how do you get it to print out all the chars take the four suits of the cards chars put them in an array and randomly pick one from that array and print it out. i have made a pretty desperate attempt at it and this is what i got.
BTW this is just trying to print all of the chars so i can pick the four.
Java Code:package hello; public class hello2 { public static void main(String[] args) { char card=' '; int lcv; for(lcv=0;lcv<=card;lcv++) { System.out.print(card+" "); } } }
Are you suggesting that Cocunuts migrate?!! -Monty Python
- 01-05-2010, 01:45 AM #2Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 7
If you want a char array you should use something like:
Java Code:char[] carray = new char[5]; char[0] = 'i'; char[1] = 'q'; char[2] = 'T'; etc. OR char[] carray = {'i','q','T','r','4'};
Java Code:String s = "hello"; char char1 = s.charAt(0); char char2 = s.charAt(1); etc.
I die a little on the inside...
Every time I get shot.
that helps a little bit but there are four chars that are the four card suits. im trying to put those in an array and then randomly pick one of them from that array. but i dont how to get those chars so i thought i would use a for loop to print out all of the chars then i could get those four and store them
hope that clears up the questionAre you suggesting that Cocunuts migrate?!! -Monty Python
Usually with Cards, we use enums for the suits and the values, and then create a Card class that holds a Suit and a value field. Since with most playing card apps, you usually randomly remove a card from a Deck, meaning you can't pick the same card twice (unless your deck holds duplicates such as in casino blackjack) the best collection for this is to place your Card objects into an ArrayList<Card>. Then to randomize this deck, you shuffle it with Collections.shuffle(...). Then a call to remove on the arraylist will both return a Card object and remove it from the list. Since this is such a common assignment, there are lots of code examples of this to be found in this and in the other major Java fora.
i have never used enums before are they different from classes?
Are you suggesting that Cocunuts migrate?!! -Monty Python
- 01-05-2010, 02:10 AM #6Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 7
Including a card example. :)
It's better if you use them, for clearity and such, but not necessary.
To pick something random, use Math.random(), which returns a random double number between zero and one (excluding 1). Than use:
Java Code:int randNumb = (int)(Math.random()*4)
Good night :)I die a little on the inside...
Every time I get shot.
so could i use math.random on an array of chars
Are you suggesting that Cocunuts migrate?!! -Monty Python
- 01-05-2010, 12:48 PM #8Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 7
Java Code:char randomChar = charArray[(int)(Math.random()*charArray.length)];
I die a little on the inside...
Every time I get shot.
- 01-05-2010, 01:05 PM #9gcampton Guest
I just stumbled across this post and funnily enough this is what I was doing today, as I have only ever made the basic String Cards. While I don't particularly want to solve the solution for you I will post some sample code so you can get an idea of how I solved this problem. (generic and simple is the key)
An enum is usually in a class of it's own, this makes it easier to use as a type and compilers prefer it this way.
Java Code:public enum Suit { HEARTS, DIAMONDS, CLUBS, SPADES }
Java Code:public class Deck { private ArrayList<Cards> aDeck; public Deck(ArrayList<Cards> deck) { setDeck(deck); populateDeck(); } /** * Set by constructor * @param aDeck sets the current ArrayList */ private void setDeck (ArrayList<Cards> aDeck) { this.aDeck = aDeck; } /** * @return the ArrayList */ public ArrayList<Cards> getDeck() { return aDeck; } /** * Populates the deck of cards Ace-King of each suit. */ public void populateDeck() { getDeck().add(new Cards(Suit.valueOf("SPADES"),Value.valueOf("ACE"))); getDeck().add(new Cards(Suit.valueOf("SPADES"),Value.valueOf("KING"))); getDeck().add(new Cards(Suit.valueOf("SPADES"),Value.valueOf("QUEEN"))); getDeck().add(new Cards(Suit.valueOf("SPADES"),Value.valueOf("JACK"))); getDeck().add(new Cards(Suit.valueOf("SPADES"),Value.valueOf("TEN"))); ... ... // and so on...
Java Code:public class Cards { private Suit suit; // create a private enum of Suit for setters/getters private Value value; public Cards(Suit s, Value v) { setSuit(s); setValue(v); } /** * @param suit sets the current card suit */ public void setSuit(Suit suit) { this.suit = suit; } // typical setters and getters public Suit getSuit() public void setValue(Value value) public Value getValue() @Override public String toString() { return String.format("%s of %s", getValue(), getSuit()); } }
Java Code:enum Apple { A(10), B(9), C, D(15), E(8); private int price; // price of each apple // Constructor Apple(int p) { price = p; } // Overloaded constructor Apple() { price = -1; } int getPrice() { return price; } }
Last edited by gcampton; 01-05-2010 at 01:41 PM.
Similar Threads
Searching index with special chars
By noorws in forum LuceneReplies: 1Last Post: 03-29-2010, 09:13 PM -
filter special chars
By Strubbl in forum LuceneReplies: 0Last Post: 08-17-2009, 10:23 AM -
Replacing the chars within a string.
By Mayur in forum New To JavaReplies: 2Last Post: 03-27-2009, 05:00 AM -
By whosadork in forum New To JavaReplies: 6Last Post: 10-03-2008, 09:40 PM -
Can we append more than 255 chars to <a href>??
By freddieMaize in forum Advanced JavaReplies: 22Last Post: 07-18-2008, 04:04 PM


text:
Still looking ;-) Though poorly written, the book has opened a whole new and different world for me. Oh, and I'd like my Mr Grey with just vanilla. LOL
Duff man.
Men like Christian Grey don't exist anymore.

Men aren't men anymore.

It makes me sad.
There was this guy in Year 12 that used to be my very own Grey. He was so oh heavenly handsome. He had that walk, that 'Im gorgeous' smile and a sleepy voice, with sleepy bed hair... We made out a few times but that was it ;(.
ooh I wish haha.... Guess I better kick in my stalker side.... O.O
No one.haven't read the the book yet.some people say that book's for old granny's (no harm intended)
Sorry...... Nah i'm just joking but i guess i'ts gotta be something sexually erotic that turn women on huh? Well good luck reading it.
Pokerboy *drools*
Related Questions
Menasay 22-25, F 18 Answers 6 Nov 25, 2012 in Movies & TV
gorgor 18-21, F 5 Answers 3 Jul 12, 2012
ss1993 18-21, F 11 Answers 5 Jul 11, 2013 in Movies & TV
I'm thinking Bradley Cooper and Ashley Greene.
SkatingMomma 41-45, F 8 Answers 3 Oct 25, 2012 in Community
HimitsuDesu 16-17, F 4 Answers 2 Nov 1, 2012 in Health
SiennaRain 26-30, F 9 Answers 3 Jul 25, 2012
stephan1994 18-21, M 1 Answer 1 Sep 5, 2012 in Dating & Relationships
Captain0bvious 22-25, M 10 Answers 0 Jul 24, 2014 in Movies & TV

subset: commoncrawl


text:
0 Item(s)
Ofna replacement engine mount kit is constructed of aluminum. All the hardware needed to install.
INCLUDES: Two engine mounts
Four 5mm phillips head screws
Four 3mm hex head screws
Four 3mm nylon locknuts
Four 5mm washers
SPECS: Length of mounts 36mm (1.42")
Height of mounts 17mm (0.66")
Distance between mounting holes from center to center is 23mm (0.91")


text:
Product Information
Product Information
Care Bears Puzzle Book
10 page padded board storybook includes 5 jigsaw puzzles. Each puzzle piece is color coded so that children can easily find which puzzle piece belongs to which puzzle. The puzzle also features the picture of the completed puzzle so a young child can easily match where each piece belongs. Suitable for young children ages 3+. Dimensions are 10?Wx10?Hx1?D.