⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dealcards.java

📁 JAVA 2入门经典 练习答案
💻 JAVA
字号:
// Chapter 13 Exercise 2

import java.util.Vector;
import java.util.LinkedList;
import java.util.Random;
import java.util.ListIterator;

public class DealCards {
  public static void main(String[] args) {
    String[] suits = {"C", "D", "H", "S"};
    String[] cardValues = { "1","2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};

    int cardsInDeck = 52;
    Vector deck = new Vector(cardsInDeck);
    LinkedList shuffledDeck = new LinkedList();
    Random chooser = new Random();       // Card chooser

    // Load the deck
    for(int i = 0 ; i< suits.length ; i++)
      for(int j = 0 ; j<cardValues.length ; j++) {
        deck.add(cardValues[j]+suits[i]);
      }

    // Select cards at random from the deck to transfer to shuffled deck
    int selection = 0;                        // Selected card index
    for(int i = 0 ; i<cardsInDeck ; i++) {
      selection = chooser.nextInt(deck.size());
      shuffledDeck.add(deck.remove(selection));
    }
    
    // Deal the cards from the shuffled deck into four hands
    StringBuffer[] hands = { new StringBuffer("Hand 1:"), new StringBuffer("Hand 2:"),
                             new StringBuffer("Hand 3:"), new StringBuffer("Hand 4:")};
    ListIterator cards = shuffledDeck.listIterator();
   
    while(cards.hasNext())
      for(int hand = 0 ; hand<hands.length ; hand++) {
      hands[hand].append(' ').append((String)(cards.next()));
    }

    // Display the hands
    for(int hand = 0 ; hand<hands.length ; hand++)
        System.out.println(hands[hand].toString());
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -