// Card shuffling and dealing program.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class DeckOfCards extends JFrame {
private Card deck[];
private int currentCard;
private JButton dealButton, shuffleButton;
private JTextField displayField;
private JLabel statusLabel; // set up deck of cards and GUI
public DeckOfCards()
{
super( "Card Dealing Program" ); 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;
// populate deck with Card objects
for ( int count = 0; count < deck.length; count++ )
deck[ count ] = new Card( faces[ count % 13 ],
suits[ count / 13 ] ); // set up GUI and event handling
Container container = getContentPane();
container.setLayout( new FlowLayout() ); dealButton = new JButton( "Deal card" );
dealButton.addActionListener( new ActionListener() { // anonymous inner class // deal one card
public void actionPerformed( ActionEvent actionEvent )
{
Card dealt = dealCard(); if ( dealt != null ) {
displayField.setText( dealt.toString() );
statusLabel.setText( "Card #: " + currentCard );
}
else {
displayField.setText( "NO MORE CARDS TO DEAL" );
statusLabel.setText( "Shuffle cards to continue" );
}
} } // end anonymous inner class ); // end call to addActionListener container.add( dealButton ); shuffleButton = new JButton( "Shuffle cards" );
shuffleButton.addActionListener( new ActionListener() { // anonymous inner class // shuffle deck
public void actionPerformed( ActionEvent actionEvent )
{
displayField.setText( "SHUFFLING ..." );
shuffle();
displayField.setText( "DECK IS SHUFFLED" );
} } // end anonymous inner class ); // end call to addActionListener container.add( shuffleButton ); displayField = new JTextField( 20 );
displayField.setEditable( false );
container.add( displayField ); statusLabel = new JLabel();
container.add( statusLabel ); setSize( 275, 120 ); // set window size
setVisible( true ); // show window
} // shuffle deck of cards with one-pass algorithm
private void shuffle()
{
currentCard = -1; // for each card, pick another random card and swap them
for ( int first = 0; first < deck.length; first++ ) {
int second = ( int ) ( Math.random() * 52 );
Card temp = deck[ first ];
deck[ first ] = deck[ second ];
deck[ second ] = temp;
} dealButton.setEnabled( true );
} // deal one card
private Card dealCard()
{
if ( ++currentCard < deck.length )
return deck[ currentCard ];
else {
dealButton.setEnabled( false );
return null;
}
} // execute application
public static void main( String args[] )
{
DeckOfCards application = new DeckOfCards(); application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
} } // end class DeckOfCards// class to represent a card
class Card {
private String face;
private String suit; // constructor to initialize a card
public Card( String cardFace, String cardSuit )
{
face = cardFace;
suit = cardSuit;
} // return String represenation of Card
public String toString()
{
return face + " of " + suit;
}} // end class Card
修改上面的程序,使card-dealing方法处理手中的五张牌(拖拉机)。写一个方法决定牌是否包括:
a) 一对
b) 两对
c) 三张一样(例:三个)
d) 四张一样(例:四个)
e) 清一色(例:
f) 一条龙(可以不同色)
g) 两张一样,另三张一样。
很着急!谢谢!!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class DeckOfCards extends JFrame {
private Card deck[];
private int currentCard;
private JButton dealButton, shuffleButton;
private JTextField displayField;
private JLabel statusLabel; // set up deck of cards and GUI
public DeckOfCards()
{
super( "Card Dealing Program" ); 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;
// populate deck with Card objects
for ( int count = 0; count < deck.length; count++ )
deck[ count ] = new Card( faces[ count % 13 ],
suits[ count / 13 ] ); // set up GUI and event handling
Container container = getContentPane();
container.setLayout( new FlowLayout() ); dealButton = new JButton( "Deal card" );
dealButton.addActionListener( new ActionListener() { // anonymous inner class // deal one card
public void actionPerformed( ActionEvent actionEvent )
{
Card dealt = dealCard(); if ( dealt != null ) {
displayField.setText( dealt.toString() );
statusLabel.setText( "Card #: " + currentCard );
}
else {
displayField.setText( "NO MORE CARDS TO DEAL" );
statusLabel.setText( "Shuffle cards to continue" );
}
} } // end anonymous inner class ); // end call to addActionListener container.add( dealButton ); shuffleButton = new JButton( "Shuffle cards" );
shuffleButton.addActionListener( new ActionListener() { // anonymous inner class // shuffle deck
public void actionPerformed( ActionEvent actionEvent )
{
displayField.setText( "SHUFFLING ..." );
shuffle();
displayField.setText( "DECK IS SHUFFLED" );
} } // end anonymous inner class ); // end call to addActionListener container.add( shuffleButton ); displayField = new JTextField( 20 );
displayField.setEditable( false );
container.add( displayField ); statusLabel = new JLabel();
container.add( statusLabel ); setSize( 275, 120 ); // set window size
setVisible( true ); // show window
} // shuffle deck of cards with one-pass algorithm
private void shuffle()
{
currentCard = -1; // for each card, pick another random card and swap them
for ( int first = 0; first < deck.length; first++ ) {
int second = ( int ) ( Math.random() * 52 );
Card temp = deck[ first ];
deck[ first ] = deck[ second ];
deck[ second ] = temp;
} dealButton.setEnabled( true );
} // deal one card
private Card dealCard()
{
if ( ++currentCard < deck.length )
return deck[ currentCard ];
else {
dealButton.setEnabled( false );
return null;
}
} // execute application
public static void main( String args[] )
{
DeckOfCards application = new DeckOfCards(); application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
} } // end class DeckOfCards// class to represent a card
class Card {
private String face;
private String suit; // constructor to initialize a card
public Card( String cardFace, String cardSuit )
{
face = cardFace;
suit = cardSuit;
} // return String represenation of Card
public String toString()
{
return face + " of " + suit;
}} // end class Card
修改上面的程序,使card-dealing方法处理手中的五张牌(拖拉机)。写一个方法决定牌是否包括:
a) 一对
b) 两对
c) 三张一样(例:三个)
d) 四张一样(例:四个)
e) 清一色(例:
f) 一条龙(可以不同色)
g) 两张一样,另三张一样。
很着急!谢谢!!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货