时代Java,与您同行!
关注微信公众号,关注前沿技术,微信搜索:nowjava或时代Java,也可点击这里扫码关注
时代Java
首页
集册
文章
实例
项目
快讯
时代+
手册
下载
Jar查找
登录
注册
Java 创建和使用Card类和Deck类
Java 创建和使用Card类和Deck类
欢马劈雪
工程师 (已认证)
原创分享签约作者
发表于
实例源码
订阅
691
查看 / 运行 实例源码
创建和使用Card类和Deck类
实例源码:
源代码:
执行
执行中...
/** * 时代Java公众号 - N o w J a v a . c o m 提 供 **/ import java.util.*; public class DisplayDeck { public static void main(String[] args) { Deck deck = new Deck(); for (int suit = Card.DIAMONDS; suit <= Card.SPADES; suit++) { for (int rank = Card.ACE; rank <= Card.KING; rank++) { Card card = deck.getCard(suit, rank); System.out.format("%s of %s%n", card.rankToString(card.getRank()), card.suitToString(card.getSuit())); } } }/**nowjava.com - 时代Java**/ } class Deck { public static int numSuits = 4; public static int numRanks = 13; public static int numCards = numSuits * numRanks; private Card[][] cards; public Deck() { cards = new Card[numSuits][numRanks]; for (int suit = Card.DIAMONDS; suit <= Card.SPADES; suit++) { for (int rank = Card.ACE; rank <= Card.KING; rank++) { cards[suit - 1][rank - 1] = new Card(rank, suit); } } } public Card getCard(int suit, int rank) { return cards[suit - 1][rank - 1]; } } class Card { private final int rank; private final int suit; // Kinds of suits public final static int DIAMONDS = 1; public final static int CLUBS = 2; public final static int HEARTS = 3; public final static int SPADES = 4; // Kinds of ranks public final static int ACE = 1; public final static int DEUCE = 2; public final static int THREE = 3; public final static int FOUR = 4; public final static int FIVE = 5; public final static int SIX = 6; public final static int SEVEN = 7; public final static int EIGHT = 8; public final static int NINE = 9; public final static int TEN = 10; public final static int JACK = 11; public final static int QUEEN = 12; public final static int KING = 13; public Card(int rank, int suit) { assert isValidRank(rank); assert isValidSuit(suit); this.rank = rank; this.suit = suit; } public int getSuit() { return suit; } public int getRank() { return rank; } public static boolean isValidRank(int rank) { return ACE <= rank && rank <= KING; } public static boolean isValidSuit(int suit) { return DIAMONDS <= suit && suit <= SPADES; } public static String rankToString(int rank) { switch (rank) { case ACE: return "Ace"; case DEUCE: return "Deuce"; case THREE: return "Three"; case FOUR: return "Four"; case FIVE: return "Five"; case SIX: return "Six"; case SEVEN: return "Seven"; case EIGHT: return "Eight"; case NINE: return "Nine"; case TEN: return "Ten"; case JACK: return "Jack"; case QUEEN: return "Queen"; case KING: return "King"; default: //Handle an illegal argument. There are generally two //ways to handle invalid arguments, throwing an exception //(see the section on Handling Exceptions) or return null return null; } } public static String suitToString(int suit) { switch (suit) { case DIAMONDS: return "Diamonds"; case CLUBS: return "Clubs"; case HEARTS: return "Hearts"; case SPADES: return "Spades"; default: return null; } } public static void main(String[] args) { // must run program with -ea flag (java -ea ..) to // use assert statements assert rankToString(ACE) == "Ace"; assert rankToString(DEUCE) == "Deuce"; assert rankToString(THREE) == "Three"; assert rankToString(FOUR) == "Four"; assert rankToString(FIVE) == "Five"; assert rankToString(SIX) == "Six"; assert rankToString(SEVEN) == "Seven"; assert rankToString(EIGHT) == "Eight"; assert rankToString(NINE) == "Nine"; assert rankToString(TEN) == "Ten"; assert rankToString(JACK) == "Jack"; assert rankToString(QUEEN) == "Queen"; assert rankToString(KING) == "King"; assert suitToString(DIAMONDS) == "Diamonds"; assert suitToString(CLUBS) == "Clubs"; assert suitToString(HEARTS) == "Hearts"; assert suitToString(SPADES) == "Spades"; } } /* * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * /**代码未完, 请加载全部代码(NowJava.com).**/
编辑/阅读全部代码
执行结果:
Ace of Diamonds
Deuce of Diamonds
Three of Diamonds
Four of Diamonds
Five of Diamonds
Six of Diamonds
Seven of Diamonds
Eight of Diamonds
Nine of Diamonds
Ten of Diamonds
Jack of Diamonds
Queen of Diamonds
King of Diamonds
Ace of Clubs
Deuce of Clubs
Three of Clubs
Four of Clubs
Five of Clubs
Six of Clubs
Seven of Clubs
Eight of Clubs
Nine of Clubs
Ten of Clubs
Jack of Clubs
Queen of Clubs
King of Clubs
Ace of Hearts
Deuce of Hearts
Three of Hearts
Four of Hearts
Five of Hearts
Six of Hearts
Seven of Hearts
Eight of Hearts
Nine of Hearts
Ten of Hearts
Jack of Hearts
Queen of Hearts
King of Hearts
Ace of Spades
Deuce of Spades
Three of Spades
Four of Spades
Five of Spades
Six of Spades
Seven of Spades
Eight of Spades
Nine of Spades
Ten of Spades
Jack of Spades
Queen of Spades
King of Spades
本文系作者在时代Java发表,未经许可,不得转载。如有侵权,请联系nowjava@qq.com删除。
编辑于
2020-03-26 09:23:27
2020-03-26 09:23:27
分享
分享文章到朋友圈
分享文章到 QQ
分享文章到微博
复制文章链接到剪贴板
扫描二维码
关注时代Java
实例源码
实例源码
订阅
订阅专栏
Java 判断文件是否为文本文件及获取文件编码格式的方法实例
bootstrap 实例演示下拉菜单(Dropdown)插件用法。
HashSet、LinkedHashSet、TreeSet类存储元素的自动排序规则实例测试
html css 对于 body和h1设置的实例源码
Java 获取在线网页的源代码
Java HashSet添加、迭代输出字符串的完整示例代码
Java 随机整数数组
html css 设置背景图片定位并且不平铺
扫描二维码
关注时代Java
返回顶部