第三章第二十四题(游戏:抽牌)(Game: pick a card)

**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

下面是参考答案代码:

public class PickACardQuestion24 {
	public static void main(String[] args) {
		int rankOfCard,suitOfCard;
		
		rankOfCard = (int)(Math.random()*13) + 1;
		suitOfCard = (int)(Math.random()*4) + 1;
		
		// Display the card
		System.out.print("The card you picked is ");
		switch(rankOfCard)
		{
			case 1:
				System.out.print("Ace");
				break;
			case 2:
				System.out.print("2");
				break;
			case 3:
				System.out.print("3");
				break;
			case 4:
				System.out.print("4");
				break;
			case 5:
				System.out.print("5");
				break;
			case 6:
				System.out.print("6");
				break;
			case 7:
				System.out.print("7");
				break;
			case 8:
				System.out.print("8");
				break;
			case 9:
				System.out.print("9");
				break;
			case 10:
				System.out.print("10");
				break;
			case 11:
				System.out.print("Jack");
				break;
			case 12:
				System.out.print("Queen");
				break;
			case 13:
				System.out.print("King");
				break;
		}
		
		switch(suitOfCard)
		{
			case 1:
				System.out.print(" of Clubs");
				break;
			case 2:
				System.out.print(" of Diamonds");
				break;
			case 3:
				System.out.print(" of Hearts");
				break;
			case 4:
				System.out.print(" of Spades");
				break;
		}
	}
}

运行效果:
在这里插入图片描述
在这里插入图片描述

注:编写程序要养成良好习惯
如:1.文件名要用英文,具体一点
2.注释要英文
3.变量命名要具体,不要抽象(如:a,b,c等等),形式要驼峰化
4.整体书写风格要统一(不要这里是驼峰,那里是下划线,这里的逻辑段落空三行,那里相同的逻辑段落空5行等等)

你可能感兴趣的:(#,第三章课后习题答案)