时代Java,与您同行!
关注微信公众号,关注前沿技术,微信搜索:nowjava或时代Java,也可点击这里扫码关注
时代Java
首页
集册
文章
实例
项目
快讯
时代+
手册
下载
Jar查找
登录
注册
Java 卡洗牌和交易。
Java 卡洗牌和交易。
欢马劈雪
工程师 (已认证)
原创分享签约作者
发表于
实例源码
订阅
384
查看 / 运行 实例源码
卡洗牌和交易。
实例源码:
源代码:
执行
执行中...
import java.security.SecureRandom; public class Main { // execute application/**from N o w J a v a . c o m - 时 代 Java**/ public static void main(String[] args) { DeckOfCards myDeckOfCards = new DeckOfCards(); myDeckOfCards.shuffle(); // place Cards in random order // print all 52 Cards in the order in which they are dealt for (int i = 1; i <= 52; i++) { // deal and display a Card System.out.printf("%-19s", myDeckOfCards.dealCard()); if (i % 4 == 0) // output a newline after every fourth card System.out.println(); } /** 来 自 N o w J a v a . c o m - 时代Java **/ } } class Card { private final String face; // face of card ("Ace", "Deuce", ...) private final String suit; // suit of card ("Hearts", "Diamonds", ...) // two-argument constructor initializes card's face and suit public Card(String face, String suit) { this.face = face; this.suit = suit; } // return String representation of Card public String toString() { return face + " of " + suit; } } class DeckOfCards { private Card[] deck; // array of Card objects private int currentCard; // index of next Card to be dealt (0-51) private static final int NUMBER_OF_CARDS = 52; // constant # of Cards // random number generator private static final SecureRandom randomNumbers = new SecureRandom(); // constructor fills deck of Cards public DeckOfCards() { String[] faces = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"}; String[] suits = {"Hearts", "Diamonds", "Clubs", "Spades"}; deck = new Card[NUMBER_OF_CARDS]; // create array of Card objects currentCard = 0; // first Card dealt will be deck[0] // populate deck with Card objects for (int count = 0; count < deck.length; count++) deck[count] = new Card(faces[count % 13], suits[count / 13]); } public void shuffle() { currentCard = 0; for (int first = 0; first < deck.length; first++) { /**代码未完, 请加载全部代码(NowJava.com).**/
编辑/阅读全部代码
执行结果:
King of Clubs Queen of Diamonds Eight of Hearts Nine of Spades
Five of Clubs Jack of Spades Deuce of Diamonds Deuce of Clubs
Ten of Spades King of Hearts Ten of Hearts Deuce of Hearts
Queen of Spades Jack of Clubs Seven of Diamonds Ace of Hearts
Four of Clubs Six of Hearts Jack of Diamonds Four of Hearts
Nine of Hearts Nine of Clubs Five of Hearts Seven of Clubs
Eight of Clubs Four of Diamonds Eight of Spades Ten of Diamonds
Seven of Hearts King of Diamonds Deuce of Spades Six of Spades
Nine of Diamonds Ace of Diamonds Six of Clubs Seven of Spades
Five of Spades Queen of Clubs Four of Spades Ten of Clubs
Jack of Hearts Six of Diamonds Ace of Clubs Queen of Hearts
Eight of Diamonds Five of Diamonds Three of Spades Ace of Spades
Three of Diamonds Three of Hearts Three of Clubs 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
返回顶部