在oracle 的java文档里看到 扑克牌的类就把它抄了一遍:
单张扑克:
package enumtype; public class Card { private final Ranks rank; private final Suits suit; enum Suits { DIAMONDS, CLUBS, HEARTS, SPADES } enum Ranks { ACE, DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING } public Card(Ranks rank, Suits suit) { this.rank = rank; this.suit = suit; } public Suits getSuit() { return suit; } public Ranks getRank() { return rank; } public String toString() { return this.suitToString(suit) + " " + this.rankToString(rank); } public static String rankToString(Ranks 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(Suits 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(Ranks.ACE) == "Ace"; assert rankToString(Ranks.DEUCE) == "Deuce"; assert rankToString(Ranks.THREE) == "Three"; assert rankToString(Ranks.FOUR) == "Four"; assert rankToString(Ranks.FIVE) == "Five"; assert rankToString(Ranks.SIX) == "Six"; assert rankToString(Ranks.SEVEN) == "Seven"; assert rankToString(Ranks.EIGHT) == "Eight"; assert rankToString(Ranks.NINE) == "Nine"; assert rankToString(Ranks.TEN) == "Ten"; assert rankToString(Ranks.JACK) == "Jack"; assert rankToString(Ranks.QUEEN) == "Queen"; assert rankToString(Ranks.KING) == "King"; assert suitToString(Suits.DIAMONDS) == "Diamonds"; assert suitToString(Suits.CLUBS) == "Clubs"; assert suitToString(Suits.HEARTS) == "Hearts"; assert suitToString(Suits.SPADES) == "Spades"; } }
一副牌:
package enumtype; import enumtype.Card.Ranks; import enumtype.Card.Suits; public class Deck { public final Card cards[]; public Deck() { cards = new Card[(Suits.values().length * Ranks.values().length)]; int i = 0; for (Suits suit : Suits.values()) { for (Ranks rank : Ranks.values()) { cards[i] = new Card(rank, suit); i++; } } } public Card getCard(Suits suit, Ranks rank) { return cards[(suit.ordinal() * rank.ordinal())]; } public static void main(String[] args) { Deck deck = new Deck(); System.out.println(deck.getCard(Suits.CLUBS, Ranks.ACE)); } }
相关推荐
Java学习笔记-Scoket.pdf Java学习笔记-Scoket.pdf是关于Java编程语言中Socket编程的学习笔记,涵盖了Socket编程的基础知识、Java中Socket的使用、Socket通信的原理及应用等方面的内容。 Socket编程的基础知识 在...
Java学习笔记-Java后来者.exe
学习任务笔记-入门大模型(含源码+项目说明介绍).zip学习任务笔记-入门大模型(含源码+项目说明介绍).zip学习任务笔记-入门大模型(含源码+项目说明介绍).zip学习任务笔记-入门大模型(含源码+项目说明介绍).zip学习任务...
Java开发 - 尚硅谷JavaWeb学习笔记 - Part1: https://blog.csdn.net/qq_63317769/article/details/139883728 Java开发 - 尚硅谷JavaWeb学习笔记 - Part2: Java开发 - 尚硅谷JavaWeb学习笔记 - Part3: Java开发 ...
云的学习笔记-云的学习笔记系统-云的学习笔记系统源码-云的学习笔记管理系统-云的学习笔记管理系统java代码-云的学习笔记系统设计与实现-基于ssm的云的学习笔记系统-基于Web的云的学习笔记系统设计与实现-云的学习...
JAVA学习笔记-第七章 面向对象中级(二)
Java学习笔记---从入门到精通之路.pdf Java是当前最流行的编程语言之一,本笔记旨在帮助初学者从基础到精通J2EE高手之路。本笔记涵盖了Java基础知识、J2EE框架、Web开发、数据库操作、图形游戏开发等方面的知识点,...
精心整理好的-Java学习笔记-供你使用
云的学习笔记-云的学习笔记系统-云的学习笔记系统源码-云的学习笔记管理系统-云的学习笔记管理系统java代码-云的学习笔记系统设计与实现-基于ssm的云的学习笔记系统-基于Web的云的学习笔记系统设计与实现-云的学习...
Java学习笔记->1.关键字、变量以及常量.md
培训机构Java教学笔记-学习资料.zip培训机构Java教学笔记-学习资料.zip培训机构Java教学笔记-学习资料.zip培训机构Java教学笔记-学习资料.zip培训机构Java教学笔记-学习资料.zip培训机构Java教学笔记-学习资料.zip
Java学习笔记是专门为Java初学者设计的一套详尽的学习资源,旨在帮助新手快速掌握这门广泛使用的编程语言。这份笔记涵盖了从基础概念到高级特性的全面内容,是学习和复习Java知识的理想工具。 1. **Java简介** ...
这份"java学习笔记-初学者的福音"涵盖了从基础到进阶的多种概念,旨在帮助初学者系统地掌握Java编程。 首先,Java的基础部分包括语法结构。Java是一种面向对象的语言,这意味着它将数据和操作数据的方法封装在类中...