Java开发网 |
注册 |
登录 |
帮助 |
搜索 |
排行榜 |
发帖统计
|
您没有登录 |
» Java开发网 » Java SE 综合讨论区
» 编程/算法/API
打印话题 寄给朋友 订阅主题 |
作者 | Re:一道面试题,向大家请教!(把扑克数组按花型和大小排列;模仿洗牌) [Re:toupeen] |
sljliuan001
发贴: 1 积分: 0 |
于 2007-02-24 20:53
import java.awt.*; import java.applet.Applet; public class DeckOfCards extends Applet{ private Card deck[]; private int currentCard; private Button dealButton, shuffleButton; private TextField displayCard; public void init(){ String faces[] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" }; String suits[] = { "Hearts", "Diamonds", "Clubs", "Spades" }; deck = new Card[ 52 ]; currentCard = -1; for( int i = 0; i < deck.length; i++ ) deck[ i ] = new Card( faces[ i % 13 ], suits[ i / 13 ] ); dealButton = new Button( "Deal card" ); shuffleButton = new Button( "shuffle cards" ); displayCard = new TextField( 20 ); displayCard.setEditable( false ); add( dealButton ); add( shuffleButton ); add( displayCard ); } public boolean action( Event event, Object object ){ if( event.target == dealButton ){ Card dealt = dealCard(); if( dealt != null ){ displayCard.setText( dealt.toString() ); showStatus( "Card #: " + currentCard ); } else { displayCard.setText( "NO MORE CARDS TO DEAL!" ); showStatus( "Shuffle cards to continue!" ); } } else if( event.target == shuffleButton ){ displayCard.setText( "SHUFFLE CARDS...." ); showStatus( "" ); shuffle(); displayCard.setText( "DECK IS SHUFFLED!" ); } return true; } public void shuffle(){ currentCard = -1; for( int i = 0; i < deck.length; i++ ){ int j = ( int )( Math.random() * 52 ); Card temp = deck[ i ]; deck[ i ] = deck[ j ]; deck[ j ] = temp; } dealButton.enable(); } public Card dealCard(){ if( ++currentCard < deck.length ) return deck[ currentCard ]; else { dealButton.disable(); return null; } } } class Card{ private String face; private String suit; public Card( String f, String s ){ face = f; suit = s; } public String toString(){ return face + " of " + suit ; } } JAVA学习日子分享 |
话题树型展开 |
已读帖子 新的帖子 被删除的帖子 |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |