第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何洗牌用二維陣列制作的套牌

如何洗牌用二維陣列制作的套牌

慕妹3242003 2022-09-07 17:35:07
我在洗牌兩副牌時遇到問題。我使用二維數(shù)組創(chuàng)建了兩個套牌。請記住,這些是要求。我還需要使用一種名為shuffle()的方法對它們進行洗牌,該方法接收1D數(shù)組并返回1D數(shù)組。在洗牌兩副牌后,我需要從第一副牌中挑選前兩張牌,從第二副牌中挑選前兩張牌,并檢查它們是否匹配。以及獲得結(jié)果所需的洗牌次數(shù)。示例輸出:第1副牌中的兩張完全匹配卡牌是:鉆石王牌,俱樂部2張第2副牌中的兩張完全匹配卡牌是:鉆石王牌,俱樂部2張洗牌次數(shù): 387這是我們參與的項目的第二部分,下面是我試圖解決這個問題的內(nèi)容。我嘗試使用下面的代碼創(chuàng)建套牌,這有效int[][] deck = new int[2][52];for (int i = 0; i <= deck.length - 1; i++) {    for (int j = 0; j < deck[i].length; j++) {        deck[i][j] = j;    }}我寫了洗牌方法,但它似乎不起作用。public static int[] shuffle(int[] deck) {  for(int i=0; i<deck.length; i++) {    int index = (int)(Math.random()* deck.length);   int temp = deck[i];    deck[i] = deck[index];  deck[index] = temp;  }return deck;    }下面的代碼是本項目第一部分的原始代碼,在對它進行洗牌并計算獲得四個這樣的代碼之后,我們需要從一個甲板上打印四個這樣的代碼。class Main {  public static void main(String[] args) {  String[] suit = { "Spades", "Hearts", "Diamond", "Clubs" };  String[] rank = { "Ace", "1", "2", "3", "4",                  "5", "6", "7", "8", "9",                 "10", "Jack", "Queen", "King" };  int rank1, rank2, rank3, rank4, suit1, suit2, suit3, suit4, count = 0;   int[] deck = new int[52];  for (int i = 0; i < deck.length; i++) {     deck[i] = i;  }  do {    count++;    for (int i = 0; i < deck.length; i++) {       int index = (int) (Math.random() * deck.length);       int temp = deck[i];       deck[i] = deck[index];       deck[index] = temp;    }    suit1 = deck[0] / 13;    suit2 = deck[1] / 13;    suit3 = deck[2] / 13;    suit4 = deck[3] / 13;    rank1 = deck[0] % 13;    rank2 = deck[1] % 13;    rank3 = deck[2] % 13;    rank4 = deck[3] % 13;  } while (rank1 != rank2 || rank2 != rank3 || rank3 != rank4);    System.out.println(" Four-of-kind cards: " + suit[suit1] + " of "        + rank[rank1] + ", " + suit[suit2] + " of " + rank[rank2]        + ", " + suit[suit3] + " of " + rank[rank3] + ", "        + suit[suit4] + " of " + rank[rank4]        + "\n Number of shuffled times: " + count);    }}結(jié)果應(yīng)該再次如下所示。示例輸出:來自卡組1的兩張精確匹配卡是:鉆石王牌,俱樂部2卡組2中的兩張精確匹配卡牌是:鉆石王牌,俱樂部2洗牌次數(shù): 387
查看完整描述

1 回答

?
精慕HU

TA貢獻1845條經(jīng)驗 獲得超8個贊

您的方法是正確的。無需執(zhí)行另一個將接受 2D 數(shù)組的 shuffle 方法。您只需要在2D陣列的每個套牌中調(diào)用該方法即可。要做到這一點,只需:shuffleshuffle


deck[0] = shuffle(deck[0]);

deck[1] = shuffle(deck[1]);

我還更新了您的代碼,以便在出現(xiàn)其他要求時可以更輕松地進行修改。請參考以下代碼:


public class Main {

    public static String[] card; // The holder for all cards


    public static void main(String[] args) {

        card = generateCardSuits(); // generate the cards with their corresponding suits

        doPartOne();

        doPartTwo();

    }


    /**

     * Part One

     *

     */

    public static void doPartOne() {

        System.out.println("========== PART ONE ==========");

        int[] deck = generateAndShuffleInitialDeck();

        int ctr = 1;

        while (true) {

            if (deck[0] % 13 == deck[1] % 13 && deck[1] % 13 == deck[2] % 13 && deck[2] % 13 == deck[3] % 13) {

                break;

            }

            deck = shuffle(deck);

            ctr++;

        }

        System.out.println("Four-of-kind cards are: " + card[deck[0]] + " , " + card[deck[1]] + " , " + card[deck[2]]

                + " and " + card[deck[3]]);

        System.out.println("Number of shuffled times: " + ctr);

        System.out.println("==============================");

    }


    /**

     * Part Two

     *

     */

    public static void doPartTwo() {

        System.out.println("========== PART TWO ==========");

        int[][] deck = new int[2][52];

        deck[0] = generateAndShuffleInitialDeck();

        deck[1] = generateAndShuffleInitialDeck();


        int ctr = 1;

        while (deck[0][0] != deck[1][0] || deck[0][1] != deck[1][1]) {

            deck[0] = shuffle(deck[0]);

            deck[1] = shuffle(deck[1]);

            ctr++;

        }


        System.out.println("Two exact match cards from deck 1 are: " + card[deck[0][0]] + " and " + card[deck[0][1]]);

        System.out.println("Number of shuffled times: " + ctr);

        System.out.println("==============================");

    }


    /**

     * Generate an initial deck of cards and shuffle them

     *

     * @return The initial and shuffled deck of cards

     */

    public static int[] generateAndShuffleInitialDeck() {

        int[] deck = new int[52];


        for (int j = 0; j < deck.length; j++) {

            deck[j] = j;

        }

        return shuffle(deck);

    }


    /**

     * Generate the cards with their corresponding suits

     *

     * @return The deck that will serve as the mapper for the cards to their suits

     */

    public static String[] generateCardSuits() {

        String[] rank = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };

        String[] suit = { "Spades", "Hearts", "Diamond", "Clubs" };


        String[] cards = new String[52];


        for (int i = 0, j = 0, s = 0; i < cards.length; i++) {

            cards[i] = rank[j] + " of " + suit[s];

            j++;

            if (j == 13) { // Since each suit consist of 13 cards

                j = 0;

                s++;

            }

        }

        return cards;

    }


    /**

     * Shuffle the deck of cards

     *

     * @param deck

     *            the deck of cards to be shuffle

     *

     * @return The shuffled deck of cards

     */

    public static int[] shuffle(int[] deck) {

        for (int i = 0; i < deck.length; i++) {

            int index = (int) (Math.random() * deck.length);

            int temp = deck[i];

            deck[i] = deck[index];

            deck[index] = temp;

        }

        return deck;

    }

}



查看完整回答
反對 回復(fù) 2022-09-07
  • 1 回答
  • 0 關(guān)注
  • 245 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號