Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > variable might not have been initialized

Reply
Thread Tools

variable might not have been initialized

 
 
nos
Guest
Posts: n/a
 
      03-03-2004

"moon" <> wrote in message
news: m...
> Hi Andrew,
> sorry i didnt mention what i was trying to do. i want to display all
> the cards in a deck..so for example, it should print out: ace of
> clubs, two of clubs, three of clubs, ace of spades, two of spades,
> etc...

-----------------
change 20 to 52 in the 'for' loop at the bottom
this does what you want
sample output
---
7 of Hearts
2 of Diamonds
9 of Diamonds
Queen of Clubs
King of Diamonds
8 of Hearts
3 of Diamonds
Queen of Diamonds
6 of Diamonds
Jack of Hearts
Jack of Diamonds
10 of Diamonds
3 of Spades
Queen of Hearts
Jack of Clubs
2 of Hearts
5 of Clubs
Queen of Spades
5 of Spades
7 of Clubs
---
-----------------
// program that prints 20 random cards
//
// Here is a solution from the master of collections himself,
// Joshua Bloch.
// Mon Oct 6 00:39:19 CDT 2003

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

class Deal {
public static void main(String args[]) {
// Make a standard 52-card deck
String[] suit =
new String[] { "Spades", "Hearts", "Diamonds", "Clubs" };
String[] rank =
new String[] {
" Ace",
" 2",
" 3",
" 4",
" 5",
" 6",
" 7",
" 8",
" 9",
" 10",
" Jack",
"Queen",
" King" };
List deck = new ArrayList();
for (int i = 0; i < suit.length; i++) {
for (int j = 0; j < rank.length; j++) {
deck.add(rank[j] + " of " + suit[i]);
}
}
Collections.shuffle(deck);
for (int j = 0; j < 20; j++) {
System.out.println(deck.get(j));
}
}
}


 
Reply With Quote
 
 
 
 
marcus
Guest
Posts: n/a
 
      03-04-2004
Moon,
Andrew treats everyone that way. I have been programming for 26 years
in 6 or 7 languages -- I used to prefer basic, now I prefer Java. I
have been posting to newsgroups for as long as I can remember. I used
to bottom post (when posts were tiny), now I top post. It's all good.

moon wrote:
> Well, thanks for taking the time to patronize. I have been posting
> questions in various groups but have not come across anything like
> this. I am a newbie but everyone starts out shaky and theres no need
> to be so demanding. Sorry for wasting everyone's time here.
> moon


 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      03-04-2004
On 3 Mar 2004 15:35:20 -0800, Gregory A. Swarthout wrote:
> Just an observation, but a normal deck of cards has 13 ranks, not
> 12.


Here's another observation: there are 13 values from 0-12 (inclusive).

moon wrote:
> for(int j = 0; j < 12; j++)


/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
Gregory A. Swarthout
Guest
Posts: n/a
 
      03-05-2004
Gordon Beaton <> wrote in message news:<4046f1ed$>...
> On 3 Mar 2004 15:35:20 -0800, Gregory A. Swarthout wrote:
> > Just an observation, but a normal deck of cards has 13 ranks, not
> > 12.

>
> Here's another observation: there are 13 values from 0-12 (inclusive).
>
> moon wrote:
> > for(int j = 0; j < 12; j++)

>
> /gordon


*Sigh*

A further observation:

j < 12 means that only values 0-11 will be used. Ergo, use j <= 12 or
j < 13.

Greg
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
annoying error: local variable may not have been initialized Shawn Java 8 10-25-2006 03:49 PM
Incorrect "variable might not have been initialized" Lee Fesperman Java 20 05-22-2006 03:49 AM
Why this error of "variable might not have been initialized"? mandy Java 14 03-27-2006 07:26 PM
final field may not have been initialized Timo Nentwig Java 4 11-02-2003 04:53 PM
Tricky frames Question? Might never have been done! moon Javascript 1 07-09-2003 09:13 AM



Advertisments