Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区 » 编程/算法/API  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:一道面试题,向大家请教!(把扑克数组按花型和大小排列;模仿洗牌) [Re:toupeen]
sljliuan001





发贴: 1
积分: 0
于 2007-02-24 20:53 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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学习日子分享

话题树型展开
人气 标题 作者 字数 发贴时间
26875 一道面试题,向大家请教!(把扑克数组按花型和大小排列;模仿洗牌) toupeen 134 2006-12-13 17:40
26162 Re:一道面试题,向大家请教!(在线等) luozhe0107 222 2006-12-15 10:47
23911 Re:一道面试题,向大家请教!(把扑克数组按花型和大小排列;模仿洗牌) renchao775 12 2007-03-20 09:53
22043 Re:一道面试题,向大家请教!(把扑克数组按花型和大小排列;模仿洗牌) aaarong 215 2007-03-21 20:49
23565 Re:一道面试题,向大家请教!(把扑克数组按花型和大小排列;模仿洗牌) zip2007 5 2007-03-27 20:33
21920 Re:一道面试题,向大家请教!(把扑克数组按花型和大小排列;模仿洗牌) xuxiaolei 898 2007-03-28 19:49
21928 Re:一道面试题,向大家请教!(把扑克数组按花型和大小排列;模仿洗牌) xuxiaolei 374 2007-03-29 07:55
21659 Re:一道面试题,向大家请教!(把扑克数组按花型和大小排列;模仿洗牌) wxnis1 82 2007-04-01 21:49
20911 Re:一道面试题,向大家请教!(把扑克数组按花型和大小排列;模仿洗牌) suntao19830709 131 2007-04-28 13:29
22573 Re:一道面试题,向大家请教!(把扑克数组按花型和大小排列;模仿洗牌) xiezhuojun2006 10 2007-05-05 23:31
20341 Re:一道面试题,向大家请教!(把扑克数组按花型和大小排列;模仿洗牌) andy_wang_5 1451 2007-06-22 14:36
24376 Re:一道面试题,向大家请教!(在线等) ycxct 1463 2006-12-19 22:25
26038 Re:一道面试题,向大家请教!(在线等) luozhe0107 14 2006-12-22 12:27
25880 Re:一道面试题,向大家请教!(在线等) dreamsky2 130 2006-12-22 14:22
21111 Re:一道面试题,向大家请教!(在线等) liangzi232004 204 2007-06-21 16:19
26181 Re:一道面试题,向大家请教!(在线等) ycxct 25 2006-12-22 17:29
25384 Re:楼上的太烦了,搞个简单的 liushuiboy 588 2007-01-24 12:41
25462 Re:一道面试题,向大家请教!(在线等) java658 4 2007-01-26 10:32
25484 Re:一道面试题,向大家请教!(在线等) ken_chie 51 2007-01-26 12:52
24773 Re:一道面试题,向大家请教!(在线等) xiaofengtoo 113 2007-02-19 13:35
22722 Re:一道面试题,向大家请教!(把扑克数组按花型和大小排列;模仿洗牌) sljliuan001 2056 2007-02-24 20:53
22007 Re:一道面试题,向大家请教!(把扑克数组按花型和大小排列;模仿洗牌) dorrenchen 953 2007-03-19 14:09

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   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