JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java 新手區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 撲克牌_洗牌
lonely741215

數學才是王道



發文: 196
積分: 0
於 2012-02-24 19:31 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.util.List;
import java.util.Collections;
import java.util.Arrays;
 
public class DeckOfCard 
{
  private List<Card> list;
  
  public DeckOfCard()
  {
    Card[] deck = new Card[52];
    int count = 0;
    
    for( String suit : Card.Suit )
      for( String face : Card.Face )
      {
        deck[count] = new Card( face, suit);
      }
    list = Arrays.asList(deck);
    
    Collections.shuffle(list);
  }
  
  public void printCards()
  {
    for( int i = 0 ; i < list.size(); i++ )
      System.out.printf("%-19s%s", list.get(i), ( (i+1)%4 == 0 )? "\n":"" );
  }
  public static void main(String[] args)
  {
    DeckOfCard app = new DeckOfCard();
    app.printCards();
  }
}
 
class Card
{
  public static String[] Face = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight",
                                "Night", "Ten", "Jack", "Queen", "King"};
  public static String[] Suit = { "Clubs", "Diamonds", "Hearts", "Spades"};
  private String face;
  private String suit;
  
  public Card( String cardFace, String cardSuit )
  {
    face = cardFace;
    suit = cardSuit;
  }
  
  public String ToString()
  {
    return String.format("%s of %s", face, suit);
  }
}


這是改寫書上的例子,書上使用enum,我改成String,請問為什麼跑出來只是一堆的null呢?謝謝


reply to postreply to post
數學啊數學~總有一天我要狂嗑你!!
作者 Re:撲克牌_洗牌 [Re:lonely741215]
p033764653





發文: 63
積分: 0
於 2012-02-24 23:02 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
1
2
3
4
5
for (String suit : Card.Suit){
  for (String face : Card.Face) {
    deck[count++] = new Card(face, suit); //<-- 你的count沒累加
  }
}


1
2
3
4
  @Override
  public String toString() {
    return String.format("%s of %s", face, suit);
  }


toString的t是小寫,不是大寫。


reply to postreply to post
Keep walking~~
作者 Re:撲克牌_洗牌 [Re:p033764653]
lonely741215

數學才是王道



發文: 196
積分: 0
於 2012-02-24 23:08 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
感謝你~!!謝謝!

reply to postreply to post
數學啊數學~總有一天我要狂嗑你!!
» JWorld@TW »  Java 新手區

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8