如何与QUOT;洗牌"数组? [英] How to "shuffle" an array?

查看:133
本文介绍了如何与QUOT;洗牌"数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个艰难的时间试图创造一个shuffleDeck()方法。

我所要做的是创建一个将数组参数(这将是扑克牌)洗牌,并返回洗牌数组列表的方法。

这是code:

 类卡
{
    int值;
    串套装;
    字符串名称;    公共字符串的toString()
    {
        回报(名称+的+花色);
    }
}公共类PickACard
{
    公共静态无效的主要(字串[] args)
    {
        卡[] =甲板buildDeck();
        //显示甲板(甲板);        诠释选择=(INT)(的Math.random()* deck.length);
        卡挑=甲板[选择]        的System.out.println(你选择了一个+挑+出了甲板。);
        的System.out.println(在大酒杯您的卡是值得+ picked.value +点);    }    公共静态卡[] buildDeck()
    {
        的String [] =套装{俱乐部,钻石,心,黑桃};
        的String []名= {ZERO,一,二,三,四有,五,六个一,七,八,九,十, 杰克,皇后,王,王牌};        INT I = 0;
        卡[] =甲板新卡[52];        对于(一个String:西服)
        {
            为(中间体V = 2; V族= 14; v ++)
            {
                卡C =新卡();
                c.suit =秒;
                c.name =名称[V]
                如果(V == 14)
                    c.value = 11;
                否则如果(ⅴ→10)
                    c.value = 10;
                其他
                    c.value = V;                甲板[I] = C;
                我++;
            }
        }
        回到甲板上;
    }    公共静态的String [] shuffleDeck(卡[]甲板)
    {
        / **我试图得到两个索引号,并交换他们。
        我试图找出如何循环会这样它种模拟洗牌。
        * /
    }    公共静态无效displayDeck(卡[]甲板)
    {
        对于(卡C:甲板)
        {
            的System.out.println(c.value +\\ t+ C);
        }
    }
}


解决方案

如何

 列表<卡>清单= Arrays.asList(甲板);
Collections.shuffle(名单);

或者单行:

  Col​​lections.shuffle(Arrays.asList(DEC));

I am having a tough time trying to create a "shuffleDeck()" method.

What I am trying to do is create a method that will take an array parameter (which will be the deck of cards) shuffle the cards, and return the shuffled array list.

This is the code:

class Card
{
    int value;
    String suit;
    String name;

    public String toString()
    {
        return (name + " of " + suit);
    }
}

public class PickACard
{
    public static void main( String[] args)
    {   
        Card[] deck = buildDeck();
        // display Deck(deck); 

        int chosen = (int)(Math.random()* deck.length);
        Card picked = deck[chosen];

        System.out.println("You picked a " + picked + " out of the deck.");
        System.out.println("In Blackjack your card is worth " + picked.value + " points.");

    }

    public static Card[] buildDeck()
    {
        String[] suits = {"clubs", "diamonds", "hearts", "spades" };
        String[] names = {"ZERO", "ONE", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "Jack", "Queen", "King", "Ace" };

        int i = 0;
        Card[] deck = new Card[52];

        for ( String s: suits )
        {   
            for ( int v = 2; v<=14; v++)
            {
                Card c = new Card();
                c.suit = s;
                c.name = names[v];
                if ( v == 14)
                    c.value = 11;
                else if ( v>10)
                    c.value = 10;
                else
                    c.value = v; 

                deck[i] = c;
                i++;
            }
        }
        return deck; 
    }

    public static String[] shuffleDeck( Card[] deck) 
    {
        /** I have attempted to get two index numbers, and swap them. 
        I tried to figure out how to loop this so it kind of simulates "shuffling". 
        */
    }

    public static void displayDeck( Card[] deck)
    {
        for ( Card c: deck) 
        {   
            System.out.println(c.value + "\t" + c);
        }
    }
}

解决方案

How about:

List<Card> list =  Arrays.asList(deck);
Collections.shuffle(list);

Or one-liner:

Collections.shuffle(Arrays.asList(dec));

这篇关于如何与QUOT;洗牌&QUOT;数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆