**2.24(游戏:抽牌)编写程序,模拟从一副52张的牌中抽一张牌。程序应该显示牌的大小(Ace、2、3、4、5、6、7、8、9、10、Jack、Queen、King)以及牌的花色(Clubs(黑梅花)、Diamonds(红方块)、Hearts(红心)、Spades(黑桃))。 下面是这个程序的运行示例: The card you picked is Jack of Hearts
**2.24(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
参考代码:
package chapter03; import java.util.Random; public class Code_24 { public static void main(String[] args){ Random r1 = new Random(); Random r2 = new Random(); int size = r1.nextInt(13); int color = r2.nextInt(4); switch (size){ case 0:System.out.print("The card you picked is 1 of ");break; case 1:System.out.print("The card you picked is 2 of ");break; case 2:System.out.print("The card you picked is 3 of ");break; case 3:System.out.print("The card you picked is 4 of ");break; case 4:System.out.print("The card you picked is 5 of ");break; case 5:System.out.print("The card you picked is 6 of ");break; case 6:System.out.print("The card you picked is 7 of ");break; case 7:System.out.print("The card you picked is 8 of ");break; case 8:System.out.print("The card you picked is 9 of ");break; case 9:System.out.print("The card you picked is 10 of ");break; case 10:System.out.print("The card you picked is Jack of ");break; case 11:System.out.print("The card you picked is Queen of ");break; case 12:System.out.print("The card you picked is King of ");break; } switch (color){ case 0:System.out.print("Clubs");break; case 1:System.out.print("Diamonds");break; case 2:System.out.print("Hearts");break; case 3:System.out.print("Spades");break; } } } 结果显示: The card you picked is 10 of Spades Process finished with exit code 0