The problem Write a program that simulates picking a card from a deck of 52 cards. Your program should display the rank (Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King) and suit (Clubs, Diamonds, Hearts, Spades) of the card Here is a sample run of the program:
The card you picked is Jack of Hearts
Breaking it down public static void main ( String [] strings ) {
Random random = new Random ();
int cardNumber = random . ints ( 1 , 53 ). limit ( 1 ). findFirst (). getAsInt ();
System . out . print ( "The card you picked is " );
getFace ( cardNumber );
getSuit ( cardNumber );
}
private static void getSuit ( int cardNumber ) {
if ( cardNumber / 13 == 0 ) {
System . out . println ( "Clubs" );
} else if ( cardNumber / 13 == 1 ) {
System . out . println ( "Diamonds" );
} else if ( cardNumber / 13 == 2 ) {
System . out . println ( "Hearts" );
} else {
System . out . println ( "Spades" );
}
}
private static void getFace ( int cardNumber ) {
if ( cardNumber % 13 == 0 ) {
System . out . print ( "Ace of " );
} else if ( cardNumber % 13 == 10 ) {
System . out . print ( "Jack of " );
} else if ( cardNumber % 13 == 11 ) {
System . out . print ( "Queen of " );
} else if ( cardNumber % 13 == 12 ) {
System . out . print ( "King of " );
} else {
System . out . print ( cardNumber % 13 + " of " );
}
}
Output Pick a card posted by Justin Musgrove on 08 April 2016
Tagged: java, java-exercises-beginner, intro-to-java-10th-edition, and ch3
Share on: Facebook Google+