2017-03-31 73 views
1

我初學者使用toString()方法返回格式化的字符串。我打印一副撲克牌,每列都是不同的套裝。有沒有人有任何想法?下面是我的代碼有:格式化toString()

public class DeckOfCards { 
    public static void main(String[] args) { 
    int[] deck = new int[52]; 
    //String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"}; 
    String[] suits = {"Clubs","Diamonds","Hearts","Spades"}; 
    //String[] ranks = {"Ace","King", "Queen", "Jack", "10", "9", "8", "7", "6", "5", "4", "3", "2"}; 
    String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King","Ace"}; 


    // Initialize cards 
     for (int i = 0; i < deck.length; i++) { 
      deck[i] = i; 
     } 

     // Shuffle the cards 
     for (int i = 0; i < deck.length; i++) { 
      int index = (int)(Math.random() * deck.length); 
      int temp = deck[i]; 
      deck[i] = deck[index]; 
      deck[index] = temp; 
     } 

     // Display the all the cards 
     for (int i = 0; i < 52; i++) { 
      String suit = suits[deck[i]/13]; 
      String rank = ranks[deck[i] % 13]; 
      System.out.println(rank + " of " + suit); 
     } 


     System.out.println("------------------------------"); 

     for(int i=0;i<deck.length;i++){ 
      for(int j=i;j<deck.length;j++){ 
       if(deck[j]==51-i){ 
        int temp = deck[i]; 
        deck[i] = deck[j]; 
        deck[j] = temp; 
       } 
      } 
     } 

     // Display the all the cards 
     for (int i = 0; i < 52; i++) { 
      String suit = suits[deck[i]/13]; 
      String rank = ranks[deck[i] % 13]; 
      System.out.printf(rank + " of " + suit + "\t"); 
     } 

    } 
    } 

我相信會在toString()方法的主要領域是在這裏:

// Display the all the cards 
     for (int i = 0; i < 52; i++) { 
      String suit = suits[deck[i]/13]; 
      String rank = ranks[deck[i] % 13]; 
      System.out.println(rank + " of " + suit); 
     } 

我怎麼會在toString()方法使用,也實例一個可以打印這個toString()方法的對象?感謝您的時間。因此,它依次顯示在橋的比賽,我想輸出的是這樣的:

Ace of Spades  Ace of Hearts  Ace of Diamonds  Ace of Clubs 
... 
// And so on until it reaches two of clubs 

做這些方式打印它像這樣沒有一個是多餘的間隔,或者如果沒有什麼可以在被修改toString()讓它出來呢?再次感謝您的時間。

回答

0

下面是製作卡片和卡組課程的示例代碼,並打印出整個卡組。

class Deck { 
    ArrayList<Card> cards = new ArrayList<Card>(); 
    public Deck() { 
     for(int i=0; i < 52; i++) { 
      cards.add(new Card()); 
     } 
    } 

    @Override 
    public String toString() { 
     String response = ""; 
     for(int i=0; i<52; i++) 
     { 
      response = response + "\n" + i+1 + ". Card: " + cards.get(i).toString() ; 
     } 
     return response; 
    } 
} 

class Card { 

    private String suit; 
    private String rank; 

    @Override 
    public String toString() { 
     return "It's a " + suit + " " + rank; 
    } 

    public Card() { 
     Random r = new Random(); 
     int rRank = r.nextInt(12); 
     switch(rRank) { 
      case 0: { 
       this.rank = "Jack"; 
       break; 
      } 
      case 1: { 
       this.rank = "Queen"; 
       break; 
      } 
      case 11: { 
       this.rank = "King"; 
       break; 
      } 
      case 12: { 
       this.rank = "Ace"; 
       break; 
      } 
      default: { 
       this.rank = rRank + ""; //Don't blame me for it. :D 
       break; 
      } 
     } 
     int suit = r.nextInt(3); 
     switch(suit) { 
      case 0: { 
       this.suit = "Club"; 
       break; 
      } 
      case 1: { 
       this.suit = "Diamond"; 
       break; 
      } 
      case 2: { 
       this.suit = "Heart"; 
       break; 
      } 
      case 3: { 
       this.suit = "Spade"; 
       break; 
      } 
     } 

    } 

} 


public class Stackoverflow { 

    public static void main(String[] args) { 
     Deck deck = new Deck(); 
     System.out.println(deck.toString()); 
    } 

} 
1

您必須創建一個新的類卡,並有一個被覆蓋的字符串類

所以像下面

public class Cards { 

    private int numb; 
    private int suite; 

    public Cards(int suite, int numb) { 
     this.numb = numb; 
     this.suite = suite; 
    } 

    public int getNumb() { 

     if (numb == 0){ 
      return 11; 
     } 

     if (numb >= 9 && numb <=12){ 
      return 10; 
     } 

     return numb + 1; 
    } 

    @Override 
    public String toString() { 

     String arbNumb = null; 
     String arbSuite = null; 

     switch (suite){ 

     case 0: 
      arbSuite = "Heart"; 
      break; 

     case 1: 
      arbSuite = "Diamond"; 
      break; 

     case 2: 
      arbSuite = "Spades"; 
      break; 

     case 3: 
      arbSuite = "Clubs"; 
      break; 

     } 

     switch (numb){ 

     case 0: 
      arbNumb = "Ace"; 
      break; 

     case 10: 
      arbNumb = "Jack"; 
      break; 

     case 11: 
      arbNumb = "Queen"; 
      break; 

     case 12: 
      arbNumb = "King"; 
      break; 

     } 

     if (numb > 0 && numb < 10){ 
      arbNumb = String.valueOf(numb+1); 
     } 

     return arbNumb + " of " + arbSuite; 
    } 

} 

它的意思是,當你實例化類Cards,數字將當您致電yourInstanceOfClass.toString時,轉換爲相應的字符串。您可以爲套件和號碼做到這一點。

我已經分配的數字就是我將如何做,但你可以改變它們到你的偏好當然。

getNumb()班只是爲了反映我所做的數字的變化。

0

你的洗牌和Fisher-Yates shuffle類似,但不是完全相同,並且不會給你卡片所有排列的均勻分配。 (Java配套的java.util.Collections類的費雪耶茨洗牌您可以使用Collections.shuffle(list)洗牌的對象列表到隨機順序

一旦你得到每張牌的花色和等級,你可以把它們放入一個列表。:

List<Card> cards = new ArrayList<>(); 

    for (int i = 0; i < 52; i++) { 
     String suit = suits[deck[i]/13]; 
     String rank = ranks[deck[i] % 13]; 
     cards.add(new Card(suit, rank)); 
    } 

(你需要在你的文件的頂部向java.util導入這些類)。

import java.util.ArrayList; 
import java.util.List; 

toString方法創建一個類卡:

public class Card { 
    private final String suit; 
    private final String rank; 

    public Card(String suit, String rank) { 
     this.suit = suit; 
     this.rank = rank; 
    } 

    public String getSuit() { 
     return suit; 
    } 

    public String getRank() { 
     return rank; 
    } 

    @Override 
    public String toString() { 
     return rank + " of " + suit; 
    } 
} 

我已經包括了一些你並不嚴格需要的東西。這些字段不一定是final,但稍後可以使用不變的對象。一旦你創建了鑽石王后,它不需要永遠變成另一張牌。一個好的經驗法則是,除非真的需要,否則不要讓對象變化。 (一張可變卡片的字段不是final,並且會爲字段提供setter,而不僅僅是getter。)儘管我們還沒有在這裏使用它們,但我也爲這些字段提供了getters。並且我在toString方法中包含了@Override註釋,但這並不是絕對必要的,但是如果在打算重寫某個方法時包含它,編譯會在您不注意時發出警告,如果我不小心寫入了tostring而不是toString

現在,我們有我們的cards,我們可以讓他們自己打印出來使用他們toString方法:

for (Card card : cards) { 
    System.out.println(card); 
} 

一個最後一個音符。在你的代碼中,你沒有正確使用System.out.printf。如果你想使用類似的東西在你的toString方法,存在StringString.format相關的方法,它的使用應該是這樣的:

@Override 
    public String toString() { 
     return String.format("%s of %s", rank, suit); 
    } 

或者,當您打印卡片,你可以使用它像這樣(%n產生一個換行符):

for (Card card : cards) { 
    System.out.printf("Your card is the %s%n", card); 
} 
1

如果你想實例的對象,這是我的解決方案:

Card.java

/*-*************************************************************************** 
* Copyright (C) 2017 Miguel Fernandez Fernandez 
* 
* This is free software, licensed under the GNU General Public License v3. 
* See http://www.gnu.org/licenses/gpl.html for more information. 
****************************************************************************/ 

public class Card { 

    String suit; 
    String rank; 

    public Card(String suit, String rank) { 
     this.suit = suit; 
     this.rank = rank; 
    } 

    public String getSuit() { 
     return suit; 
    } 

    public void setSuit(String suit) { 
     this.suit = suit; 
    } 

    public String getRank() { 
     return rank; 
    } 

    public void setRank(String rank) { 
     this.rank = rank; 
    } 

    @Override 
    public String toString() { 
     return rank + "\t" + suit; 
    } 
} 

Main.java

/*-*************************************************************************** 
* Copyright (C) 2017 Miguel Fernandez Fernandez 
* 
* This is free software, licensed under the GNU General Public License v3. 
* See http://www.gnu.org/licenses/gpl.html for more information. 
****************************************************************************/ 

import java.util.ArrayList; 
import java.util.Collections; 
import java.util.List; 

public class Main { 

    public static void main(String[] args) { 

     List<Card> deck = new ArrayList<Card>(52); 
     String[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" }; 
     String[] ranks = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" }; 

     // Initialize cards 
     for (int i = 0; i < 4; i++) { 
      for (int j = 0; j < 13; j++) { 
       deck.add(new Card(suits[i], ranks[j])); 
      } 
     } 

     System.out.println("Cards: \n"); 

     // Print cards 
     for (Card c : deck) { 
      System.out.println(c.toString()); 
     } 

     System.out.println("\n\nRandom cards...\n\n"); 

     // Shuffle the cards 
     Collections.shuffle(deck); 
     System.out.println("Cards: \n"); 

     // Display the all the cards 
     for (Card c : deck) { 
      System.out.println(c.toString()); 
     } 
    } 
} 

謝謝! :)