HW 6 – Game: pick a card Write a program that simulates picking a card from a de
ID: 3904820 • Letter: H
Question
HW 6 – Game: pick a card 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 Another run: The card you picked is 3 of Diamonds Hint: You can use rand() function and generate two random integers. The first random number should be from 0 to 10 (for the 11 possible ranks) and the second should be from 0 to 3 (for the 4 possible suits). Then use switch-case to show a proper message based on the values of the two random integer numbers generated.
Explanation / Answer
import java.io.*;
import java.util.*;
public class DemoCard1{
public static void pickCard(){
String[] suit = {"Diamonds","Hearts","Clubs","Spades"};
String[] label = {"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};
Random rand = new Random();
int l = rand.nextInt(13);
int s = rand.nextInt(4);
System.out.print("The card you picke is ");
switch(l) {
case 0 : System.out.print("Ace");
break;
case 1 : System.out.print("2");
break;
case 2 : System.out.print("3");
break;
case 3 : System.out.print("4");
break;
case 4 : System.out.print("5");
break;
case 5 : System.out.print("6");
break;
case 6 : System.out.print("7");
break;
case 7 : System.out.print("8");
break;
case 8 : System.out.print("9");
break;
case 9 : System.out.print("10");
break;
case 10 : System.out.print("Jack");
break;
case 11 : System.out.print("Queen");
break;
case 12 : System.out.print("King");
break;
}
switch(s) {
case 0 : System.out.println(" of Diamonds");
break;
case 1 : System.out.println(" of Hearts");
break;
case 2 : System.out.println("Clubs");
break;
case 3 : System.out.println("Spades");
}
}
public static void main(String[] args){
pickCard();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.