如何才能显示阵列中的所有卡片?

时间:2018-04-30 04:34:03

标签: java arrays tostring

输出很奇怪。这是一个类,我需要以某种方式改变toString方法以使其工作,但我不知道如何。这给了我我认为的内存位置,如Card @ wods903je0d等等。我创建了一个包含所有卡片的阵列,但我不知道如何显示它们。我需要帮助。

\\\\\\\\\\\\\\\\\\ \\\\\\\\\\\\\\\\\\ \\\\\\\\\\\\\\\\\\ \\\\\\\\\\\\\\\\\\ \\\\\\\\\\\\\\\\\\ \\\\\\\\\\\\\\\\\\ \\\\\\\\\\\\\\\\\\ \\\\\\\\\\\\\\\\\\ \\\\\\\\\\\\\\\\\\

// wblab17.java
// This is the Student starting version for the <Deck> class lab assignment. 


public class wblab17100
{
   public static void main(String args[])
   {
      System.out.println("Lab11bst.JAVA");
      System.out.println();     
      Deck deck = new Deck();
      System.out.println(deck);
   }
}

class Card {
   private String suit; 
   private String rank; 
   private int pointValue;
   public Card(String s, String r, int pV) {
      suit = s;
      rank = r; 
      pointValue = pV;
   }
}


class Deck 
{
   private Card[] cards;
   private int size;

   public Deck() 
   {
  size = 52;
  cards = new Card[size];
  Card n1 = new Card("Clubs", "Two", 2);
  Card n2 = new Card("Clubs", "Three", 3);
  Card n3 = new Card("Clubs", "Four", 4);
  Card n4 = new Card("Clubs", "Five", 5);
  Card n5 = new Card("Clubs", "Six", 6);
  Card n6 = new Card("Clubs", "Seven", 7);
  Card n7 = new Card("Clubs", "Eight", 8);
  Card n8 = new Card("Clubs", "Nine", 9);
  Card n9 = new Card("Clubs", "Ten", 10);
  Card n10 = new Card("Clubs", "Jack", 10);
  Card n11 = new Card("Clubs", "Queen", 10);
  Card n12 = new Card("Clubs", "King", 10);
  Card n13 = new Card("Clubs", "Ace", 11);
  Card n14 = new Card("Diamonds", "Two", 2);
  Card n15 = new Card("Diamonds", "Three", 3);
  Card n16 = new Card("Diamonds", "Four", 4);
  Card n17 = new Card("Diamonds", "Five", 5);
  Card n18 = new Card("Diamonds", "Six", 6);
  Card n19 = new Card("Diamonds", "Seven", 7);
  Card n20 = new Card("Diamonds", "Eight", 8);
  Card n21 = new Card("Diamonds", "Nine", 9);
  Card n22 = new Card("Diamonds", "Ten", 10);
  Card n23 = new Card("Diamonds", "Jack", 10);
  Card n24 = new Card("Diamonds", "Queen", 10);
  Card n25 = new Card("Diamonds", "King", 10);
  Card n26 = new Card("Diamonds", "Ace", 11);
  Card n27 = new Card("Hearts", "Two", 2);
  Card n28 = new Card("Hearts", "Three", 3);
  Card n29 = new Card("Hearts", "Four", 4);
  Card n30 = new Card("Hearts", "Five", 5);
  Card n31 = new Card("Hearts", "Six", 6);
  Card n32 = new Card("Hearts", "Seven", 7);
  Card n33 = new Card("Hearts", "Eight", 8);
  Card n34 = new Card("Hearts", "Nine", 9);
  Card n35 = new Card("Hearts", "Ten", 10);
  Card n36 = new Card("Hearts", "Jack", 10);
  Card n37 = new Card("Hearts", "Queen", 10);
  Card n38 = new Card("Hearts", "King", 10);
  Card n39 = new Card("Hearts", "Ace", 11);
  Card n40 = new Card("Spades", "Two", 2);
  Card n41 = new Card("Spades", "Three", 3);
  Card n42 = new Card("Spades", "Four", 4);
  Card n43 = new Card("Spades", "Five", 5);
  Card n44 = new Card("Spades", "Six", 6);
  Card n45 = new Card("Spades", "Seven", 7);
  Card n46 = new Card("Spades", "Eight", 8);
  Card n47 = new Card("Spades", "Nine", 9);
  Card n48 = new Card("Spades", "Ten", 10);
  Card n49 = new Card("Spades", "Jack", 10);
  Card n50 = new Card("Spades", "Queen", 10);
  Card n51 = new Card("Spades", "King", 10);
  Card n52 = new Card("Spades", "Ace", 11);

  cards[0]= n1;
  cards[1]= n2;
  cards[2]= n3;
  cards[3]= n4;
  cards[4]= n5;
  cards[5]= n6;
  cards[6]= n7;
  cards[7]= n8;
  cards[8]= n9;
  cards[9]= n10;
  cards[10]= n11;
  cards[11]= n12;
  cards[12]= n13;
  cards[13]= n14;
  cards[14]= n15;
  cards[15]= n16;
  cards[16]= n17;
  cards[17]= n18;
  cards[18]= n19;
  cards[19]= n20;
  cards[20]= n21;
  cards[21]= n22;
  cards[22]= n23;
  cards[23]= n24;
  cards[24]= n25;
  cards[25]= n26;
  cards[26]= n27;
  cards[27]= n28;
  cards[28]= n29;
  cards[29]= n30;
  cards[30]= n31;
  cards[31]= n32;
  cards[32]= n33;
  cards[33]= n34;
  cards[34]= n35;
  cards[35]= n36;
  cards[36]= n37;
  cards[37]= n38;
  cards[38]= n39;
  cards[39]= n40;
  cards[40]= n41;
  cards[41]= n42;
  cards[42]= n43;
  cards[43]= n44;
  cards[44]= n45;
  cards[45]= n46;
  cards[46]= n47;
  cards[47]= n48;
  cards[48]= n49;
  cards[49]= n50;
  cards[50]= n51;
  cards[51]= n52;
   }

   public String toString(){
      String arr = "";
      for(int i = 0; i<size; i++){
         arr = arr + cards[i].toString() +"\n";
      }
      return arr;
   }    
 }

2 个答案:

答案 0 :(得分:1)

您的代码问题是您在Deck类中覆盖toString()方法,而您应该在Card类中重写。由于您没有覆盖Card类的toString()方法,因此将执行Object类中提供的实现,该实现将打印给定对象的内存位置。如果要打印对象的字段值,请根据打印逻辑覆盖Card类中的toString()方法。下面是覆盖Card类的toString方法的方法之一。

@Override
public String toString() {
    return "Card{" +
            "suit= '" + suit + '\'' +
            ", rank= '" + rank + '\'' +
            ", pointValue=" + pointValue +
            '}';
}

答案 1 :(得分:0)

toString()课程中提供Card方法。

@Override
public String toString() {
    return "Card{" +
            "suit='" + suit + '\'' +
            ", rank='" + rank + '\'' +
            ", pointValue=" + pointValue +
            '}';
}