import acm.program.*;
import acm.util.*;
public class Exercises extends ConsoleProgram {
public void run() {
println("This program displays a randomly chosen card.");
println(card(rgen.nextInt(1,13)) + " of " + suit(rgen.nextInt(1,4)));
} //ends run
private String card(int number) {
switch (number) {
case 1: return "Ace";
case 2: return "2";
case 3: return "3";
case 4: return "4";
case 5: return "5";
case 6: return "6";
case 7: return "7";
case 8: return "8";
case 9: return "9";
case 10: return "10";
case 11: return "Jack";
case 12: return "Queen";
case 13: return "King";
default: return null;
}//ends switch
}//ends method
private String suit(int number) {
switch (number) {
case 1: return "Clubs";
case 2: return "Diamonds";
case 3: return "Hearts";
case 4: return "Spades";
default: return null;
}
}
private RandomGenerator rgen = new RandomGenerator();
}//ends class
Between Stanford CS106A and 'Art and Science of Java' (the course textbook.
Just showing people some of the code, not all of it though. Only when I feel like it.
Monday, August 5, 2013
Write a program that displays the name of a card randomly chosen from a complete
deck of 52 playing cards. Each card consists of a rank (Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10,
Jack, Queen, King) and a suit (Clubs, Diamonds, Hearts, Spades). Your program
should display the complete name of the card
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment