簡體   English   中英

如何顯示陣列中的所有卡?

[英]How can I get all the card in the array to show?

輸出很奇怪。 這是針對一個類的,我需要以某種方式更改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;
   }    
 }

代碼的問題在於,您應該在Deck類中覆蓋toString()方法,而應該在Card類中進行覆蓋。 由於尚未為Card類重寫toString()方法,因此將執行Object類中提供的實現,該實現將打印給定對象的內存位置。 如果要打印對象的字段值,請按照打印邏輯重寫Card類中的toString()方法。 下面是重寫Card類的toString方法的一種方法。

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

Card類中提供toString()方法。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM