使用数组创建DeckOfCards类

时间:2012-03-13 17:50:32

标签: java arrays playing-cards

过去几天我一直在与此作斗争,我希望有人可以提供一些指导。第一次使用Java中的数组,只用了大约2个月的时间学习Java。要温柔。 :)

在我看来,我正在创建一个并行数组,其中cardInDeck [] []正在填充cardName(例如,Ace,10,Jack)和suitName(例如,Hearts,Spades等)。虽然我的方法输出和我认为〜我正在写的数组正如预期的那样,但是当打印出数组的内容时,我会得到一些异常结果。

我试图缩小范围,如果那些意外的结果是因为我在声明它时不正确地构造数组,如果我正在尝试创建一个对上帝和自然(或至少是Java)可憎的数组或者如果数组是正确的,但我不知道如何打印内容。

您可以提供的任何帮助将不胜感激。提前谢谢!

代码和结果如下所示。

import java.util.*;
import java.lang.*;
//
public class DeckOfCards
{
   public static void main(String[] args)
   {
      int rank;
      int suit;
//
   for(rank=1;rank<14;rank++)
   {
       String tempCardName = Integer.toString(rank);
       String cardName;
       if(rank==1)
       cardName="Ace";
       else
       if(rank==13)
       cardName="King";
       else
       if(rank==12)
       cardName="Queen";
       else
       if(rank==11)
       cardName="Jack";
       else
       cardName=tempCardName;
 //
       for(suit=1;suit<5;suit++)
       {
       String suitName;
       switch(suit)
       {case 1:
   suitName = "Spades";
   break;
   case 2:
   suitName = "Hearts";
   break;
   case 3:
   suitName = "Clubs";
   break;
         case 4:
   suitName = "Diamonds";
   break;
   default:
   suitName = "Error";}
       String tempCardInDeck=(cardName + " of " + suitName);
       System.out.print("method output: " tempCardInDeck);
 //*
       String[][] cardInDeck = new String[][]
          {
             {cardName,suitName}
          };
       System.out.print(" /// to array: " + cardName + " " + suitName);
       System.out.println(" /// from array: " + Arrays.toString( cardInDeck ) );
     }
     }
   }
 }

结果:

method output: Ace of Spades /// to array: Ace Spades /// from array: [[Ljava.lang.String;@9ffb18]
method output: Ace of Hearts /// to array: Ace Hearts /// from array: [[Ljava.lang.String;@11de914]
method output: Ace of Clubs /// to array: Ace Clubs /// from array: [[Ljava.lang.String;@b1406b]
method output: Ace of Diamonds /// to array: Ace Diamonds /// from array: [[Ljava.lang.String;@fc5408]
method output: 2 of Spades /// to array: 2 Spades /// from array: [[Ljava.lang.String;@1f102c1]
method output: 2 of Hearts /// to array: 2 Hearts /// from array: [[Ljava.lang.String;@1ec0130]
method output: 2 of Clubs /// to array: 2 Clubs /// from array: [[Ljava.lang.String;@1420fea]
method output: 2 of Diamonds /// to array: 2 Diamonds /// from array: [[Ljava.lang.String;@230be4]
method output: 3 of Spades /// to array: 3 Spades /// from array: [[Ljava.lang.String;@e1e567]
method output: 3 of Hearts /// to array: 3 Hearts /// from array: [[Ljava.lang.String;@9bfee2]
method output: 3 of Clubs /// to array: 3 Clubs /// from array: [[Ljava.lang.String;@17aaeec]
method output: 3 of Diamonds /// to array: 3 Diamonds /// from array: [[Ljava.lang.String;@1721a26]
method output: 4 of Spades /// to array: 4 Spades /// from array: [[Ljava.lang.String;@12db7c]
method output: 4 of Hearts /// to array: 4 Hearts /// from array: [[Ljava.lang.String;@7c28c]
method output: 4 of Clubs /// to array: 4 Clubs /// from array: [[Ljava.lang.String;@17588d5]
method output: 4 of Diamonds /// to array: 4 Diamonds /// from array: [[Ljava.lang.String;@16a7c99]
method output: 5 of Spades /// to array: 5 Spades /// from array: [[Ljava.lang.String;@1a5d08]
method output: 5 of Hearts /// to array: 5 Hearts /// from array: [[Ljava.lang.String;@d1c9b5]
method output: 5 of Clubs /// to array: 5 Clubs /// from array: [[Ljava.lang.String;@111c3f0]
method output: 5 of Diamonds /// to array: 5 Diamonds /// from array: [[Ljava.lang.String;@156f14c]
method output: 6 of Spades /// to array: 6 Spades /// from array: [[Ljava.lang.String;@fbd1fc]
method output: 6 of Hearts /// to array: 6 Hearts /// from array: [[Ljava.lang.String;@973678]
method output: 6 of Clubs /// to array: 6 Clubs /// from array: [[Ljava.lang.String;@1791620]
method output: 6 of Diamonds /// to array: 6 Diamonds /// from array: [[Ljava.lang.String;@9ba632]
method output: 7 of Spades /// to array: 7 Spades /// from array: [[Ljava.lang.String;@bc5245]
method output: 7 of Hearts /// to array: 7 Hearts /// from array: [[Ljava.lang.String;@1bd523d]
method output: 7 of Clubs /// to array: 7 Clubs /// from array: [[Ljava.lang.String;@6250d2]
method output: 7 of Diamonds /// to array: 7 Diamonds /// from array: [[Ljava.lang.String;@a8198c]
method output: 8 of Spades /// to array: 8 Spades /// from array: [[Ljava.lang.String;@25753d]
method output: 8 of Hearts /// to array: 8 Hearts /// from array: [[Ljava.lang.String;@1341183]
method output: 8 of Clubs /// to array: 8 Clubs /// from array: [[Ljava.lang.String;@169cccc]
method output: 8 of Diamonds /// to array: 8 Diamonds /// from array: [[Ljava.lang.String;@10469e8]
method output: 9 of Spades /// to array: 9 Spades /// from array: [[Ljava.lang.String;@c4fedd]
method output: 9 of Hearts /// to array: 9 Hearts /// from array: [[Ljava.lang.String;@138847d]
method output: 9 of Clubs /// to array: 9 Clubs /// from array: [[Ljava.lang.String;@1826ac5]
method output: 9 of Diamonds /// to array: 9 Diamonds /// from array: [[Ljava.lang.String;@12fb063]
method output: 10 of Spades /// to array: 10 Spades /// from array: [[Ljava.lang.String;@1e55d39]
method output: 10 of Hearts /// to array: 10 Hearts /// from array: [[Ljava.lang.String;@14b525c]
method output: 10 of Clubs /// to array: 10 Clubs /// from array: [[Ljava.lang.String;@c4c05]
method output: 10 of Diamonds /// to array: 10 Diamonds /// from array: [[Ljava.lang.String;@1530551]
method output: Jack of Spades /// to array: Jack Spades /// from array: [[Ljava.lang.String;@18235a1]
method output: Jack of Hearts /// to array: Jack Hearts /// from array: [[Ljava.lang.String;@18ee2ee]
method output: Jack of Clubs /// to array: Jack Clubs /// from array: [[Ljava.lang.String;@1d48043]
method output: Jack of Diamonds /// to array: Jack Diamonds /// from array: [[Ljava.lang.String;@30cd64]
method output: Queen of Spades /// to array: Queen Spades /// from array: [[Ljava.lang.String;@1fc9fee]
method output: Queen of Hearts /// to array: Queen Hearts /// from array: [[Ljava.lang.String;@67f797]
method output: Queen of Clubs /// to array: Queen Clubs /// from array: [[Ljava.lang.String;@1b01949]
method output: Queen of Diamonds /// to array: Queen Diamonds /// from array: [[Ljava.lang.String;@4c2849]
method output: King of Spades /// to array: King Spades /// from array: [[Ljava.lang.String;@1e8f2a0]
method output: King of Hearts /// to array: King Hearts /// from array: [[Ljava.lang.String;@90f19c]
method output: King of Clubs /// to array: King Clubs /// from array: [[Ljava.lang.String;@1e67280]
method output: King of Diamonds /// to array: King Diamonds /// from array: [[Ljava.lang.String;@675039]

3 个答案:

答案 0 :(得分:1)

如果要打印数组的内容,则应使用Arrays.deepToString(cardInDeck)代替Arrays.toString(cardInDeck)

答案 1 :(得分:0)

您在输出中看到的是toString的默认实现。来自Arrays的{​​{1}}:

  

返回指定数组内容的字符串表示形式。如果数组包含其他数组作为元素,则它们将从Object继承的Object.toString()方法转换为字符串,该方法描述其身份而不是其内容。

因为它是the documentation的默认版本,所以你会得到......

  

类Object的toString方法返回一个字符串,该字符串由对象为实例的类的名称,符号字符“@”和对象的哈希码的无符号十六进制表示组成。 / p>

正如另一个答案中所建议的,Object应该是你所追求的。这对我来说实际上是一个新的,所以我会为自己尝试。

答案 2 :(得分:0)

实现一副牌的另一种方法是使用枚举,这允许编译器为你强制执行类型检查,这是一种非常好的做法。有关这些链接中Oracle's Java Tutorialscardsdeck的示例,请参阅display