分裂甲板错误?真的很容易,但我搞砸了

时间:2014-01-14 23:39:22

标签: java

我试图将一副牌分成两个阵列(一个用于用户,一个用于计算机)

static String [] deck = [52 cards here]
             static String [] userDeck = new String [26];
             static String [] computerDeck = new String [26];

         public static void deckSplit ()
             {
              for (int i = 0; i <= 25; i++) {
              userDeck[i] = deck[2 * i];
              computerDeck[i] = deck[(2 * i) + 1];
              }
            }

但是我收到了这个错误:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at war.Deck.deckSplit(Deck.java:47) [the part about the computerDeck]
    at war.War.main(War.java:28) [where i called the decksplit method]

编辑:

 static String [] deck = {"2 of Diamonds" + "\n2 of Hearts"+"\n2 of Clubs"+ "\n2 of Spades"+ 
                    "\n3 of Diamonds"+ "\n3 of Hearts"+ "\n3 of Clubs"+ "\n3 of Spades"+ 
                   "\n4 of Diamonds"+ "\n4 of Hearts"+ "\n4 of Clubs"+ 
                   "\n4 of Spades"+ "\n5 of Diamonds"+ "\n5 of Hearts"+
                   "\n5 of Clubs"+ "\n5 of Spades"+ "\n6 of Diamonds"+ 
                   "\n6 of Hearts"+ "\n6 of Clubs"+ "\n6 of Spades"+
                   "\n7 of Diamonds"+ "\n7 of Hearts"+ "\n7 of Clubs"+  
                   "\n7 of Spades"+ "\n8 of Diamonds"+ "\n8 of Hearts"+  
                   "\n8 of Clubs" +"\n8 of Spades"+ "\n9 of Diamonds"+ "\n9 of Hearts"+ 
                   "\n9 of Clubs"+ "\n9 of Spades"+  "\n10 of Diamonds"+  
                   "\n10 of Hearts"+ "\n10 of Clubs" + "\n10 of Spades"+   
                   "\nJack of Diamonds"+ "\nJack of Hearts"+ "\nJack of Clubs"+ 
                   "\nJack of Spades"+ "\nQueen of Diamonds"+ "\nQueen of Hearts"+  
                   "\nQueen of Clubs"+ "\nQueen of Spades"+ "\nKing of Diamonds" +   
                    "\nKing of Hearts" + "\nKing of Clubs" + "\nKing of Spades"+   
                   "\nAce of Diamonds"+ "\nAce of Hearts"+  "\nAce of Clubs" 
                    + "\nAce of Spades"};//52

 static String [] userDeck = new String [27];
 static String [] computerDeck = new String [27];
这是52张牌。我不知道如何分解它:/

3 个答案:

答案 0 :(得分:4)

您正在创建一个大字符串:

static String [] deck = {"2 of Diamonds" + "\n2 of Hearts"+"\n2 of Clubs"+ ...

将所有字符串添加到一起

+更改为,

static String [] deck = {"2 of Diamonds" , "\n2 of Hearts"+"\n2 of Clubs" , "\n2 of 

如果您打印deck[0],您会看到我的意思。

答案 1 :(得分:1)

static String [] deck = {"2 of Diamonds" + "\n2 of Hearts"+"\n2 of Clubs"+ "\n2 of Spades"+ 
                    "\n3 of Diamonds"+ "\n3 of Hearts"+ "\n3 of Clubs"+ "\n3 of Spades"+ 
                   "\n4 of Diamonds"+ "\n4 of Hearts"+ "\n4 of Clubs"+ 
                   "\n4 of Spades"+ "\n5 of Diamonds"+ "\n5 of Hearts"+
                   "\n5 of Clubs"+ "\n5 of Spades"+ "\n6 of Diamonds"+ 
                   "\n6 of Hearts"+ "\n6 of Clubs"+ "\n6 of Spades"+
                   "\n7 of Diamonds"+ "\n7 of Hearts"+ "\n7 of Clubs"+  
                   "\n7 of Spades"+ "\n8 of Diamonds"+ "\n8 of Hearts"+  
                   "\n8 of Clubs" +"\n8 of Spades"+ "\n9 of Diamonds"+ "\n9 of Hearts"+ 
                   "\n9 of Clubs"+ "\n9 of Spades"+  "\n10 of Diamonds"+  
                   "\n10 of Hearts"+ "\n10 of Clubs" + "\n10 of Spades"+   
                   "\nJack of Diamonds"+ "\nJack of Hearts"+ "\nJack of Clubs"+ 
                   "\nJack of Spades"+ "\nQueen of Diamonds"+ "\nQueen of Hearts"+  
                   "\nQueen of Clubs"+ "\nQueen of Spades"+ "\nKing of Diamonds" +   
                    "\nKing of Hearts" + "\nKing of Clubs" + "\nKing of Spades"+   
                   "\nAce of Diamonds"+ "\nAce of Hearts"+  "\nAce of Clubs" 
                    + "\nAce of Spades"};//52

上面的代码包含一个由连接字符串组成的元素。这就是当您尝试访问不存在的第二个元素时出现异常的原因。请参阅此内容以了解应如何创建此数组:http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

快速举例:

static String [] deck = {"2 of Diamonds", "2 of Diamonds", /* etc */};

答案 2 :(得分:0)

你可以发布你如何填充套牌的代码吗?我会检查它的长度,并确保它是52,我在这里重复你想要做的最好的事情,而不用看你的代码的其余部分,这似乎有效:

import java.util.Arrays;


public class Cards {
    static String[] deck = new String[52];
    static String[] userDeck = new String[26];
    static String[] computerDeck = new String[26];

    public static void main(String[] args) {
        for (int i=0;i<52;i++) {
            Cards.deck[i] = "Card " + i;
        }
        Cards.deckSplit();
    }
    public static void deckSplit ()
    {
        System.out.println(Cards.deck.length);
         for (int i = 0; i <= 25; i++) {
             Cards.userDeck[i] = Cards.deck[2 * i];
             Cards.computerDeck[i] = Cards.deck[(2 * i) + 1];
         }
         System.out.println(Arrays.toString(Cards.userDeck));
         System.out.println(Arrays.toString(Cards.computerDeck));
         System.out.println("userDeck:" + Cards.userDeck.length);
         System.out.println("computerDeck:" + Cards.computerDeck.length);
   }

}

它输出:

52
[Card 0, Card 2, Card 4, Card 6, Card 8, Card 10, Card 12, Card 14, Card 16, Card 18, Card 20, Card 22, Card 24, Card 26, Card 28, Card 30, Card 32, Card 34, Card 36, Card 38, Card 40, Card 42, Card 44, Card 46, Card 48, Card 50]
[Card 1, Card 3, Card 5, Card 7, Card 9, Card 11, Card 13, Card 15, Card 17, Card 19, Card 21, Card 23, Card 25, Card 27, Card 29, Card 31, Card 33, Card 35, Card 37, Card 39, Card 41, Card 43, Card 45, Card 47, Card 49, Card 51]
userDeck:26
computerDeck:26