2012-03-28 44 views
4

我正在寫一個程序,做一個卡技巧,但首先我必須弄清楚如何處理3行7張卡。我無法弄清楚爲什麼當我運行我的程序時,它只輸出所有卡的一個數字,以及如何讓它打印出卡的名稱。我可以用PrintCard()函數打印52張獨特的卡片,但在使用Deal()時不能打印,我不知道爲什麼會有任何幫助。謝謝。Java卡處理程序

這是目前我的代碼:

import java.util.Random; 

public class testing { 

    public static void main(String[] args) { 
    testing cs = new testing();  
    int [] deck = new int [52]; 
    int [] [] play = new int [7] [3]; 
    cs.BuildDeck (deck); 
    cs.Deal(deck, play); 
    } 

    public static void BuildDeck(int deck[]) { 
     int[] used = new int[52]; 
     int card = 0; 
     int i = 0; 
     /* 
     * Generate cards until the deck is full of integers 
     */ 
     while (i < deck.length) { 
      /* 
      * generate a random number between 0 and 51 
      */ 
      Random random1 = new Random(); 
      card = (random1.nextInt(52) % 52); 
      /* 
      * Check the used array at the position of the card. If 0, add the card and set the used location 
      * to 1. If 1, generate another number 
      */ 
      if (used[card] == 0) { 
       used[card] = 1; 
       deck[i] = card; 
       i++; 
      } 
     } 
    } 

    public static void PrintCard(int card) { 
     int rank = (card % 13); 
     int suit = (card/13); 
     String[] suits = {"Clubs", "Hearts", "Diamonds", "Spades"}; 
     String[] ranks = {"King", "Ace", "2", "3", "4", "5", "6", 
      "7", "8", "9", "10", "Jack", "Queen"}; 

     System.out.println(ranks[rank] + " of " + suits[suit]); 
    } 

    public static void Deal(int deck[], int play[][]) { 
     int card = 0; 
System.out.println ("\n Column 0   Column 1   Column 2"); 
System.out.println ("=======================================================\n"); 
for (int row = 0; row < play.length; row++) { 
    for (int col = 0; col < play [row].length; col++) { 
     play[row][col] = deck[card++]; 
     System.out.print("  " + play[row][col] + "   "); 

    } 
    System.out.println(); 
     } 
    } 
} 

更大的計劃:http://pastebin.com/99tLpVjM

+0

雷內說什麼。但是:你的建造方式+洗牌是不理想的,因爲它不能保證完成。 'Collections.shuffle(Arrays.asList(array));'的行會是更好的解決方案。 – tim 2012-03-28 11:16:30

+0

代碼批判:從不使用主列中的'i'。 while循環中的'random1'只能初始化一次。卡應該在while循環中聲明,因爲它不在外部使用。 Deal中的'row,col,card'應該在for循環中聲明。 – 2012-03-28 11:18:29

+0

方法以大寫字母開頭! – mishadoff 2012-03-28 11:26:33

回答

0

在交易中,你永遠不會用卡做什麼。使其迭代。

public void deal (int deck [], int play [] []) { 
    /* 
    * deal cards by passing addresses of cardvalues from the deck array to the play array 
    */ 
    int card = 0; 
    System.out.println ("\n Column 0   Column 1   Column 2"); 
    System.out.println ("=======================================================\n"); 
    for (int row = 0; row < play.length; row++) { 
     for (int col = 0; col < play [row].length; col++) { 
      play [row] [col] = deck [card++]; 
      System.out.print ("\t" + printCard (card) + "\t"); 
     } 
     System.out.println(); 
    } 
} 

因此,申報隨機的一流水平:

Random random1 = new Random(); 

public static void main (String [] args) { 
    CardShuffle cs = new CardShuffle(); 
    int [] deck = new int [52]; 
    int [] [] play = new int [7] [3]; 
    cs.buildDeck (deck); 
    cs.deal (deck, play); 
} 

不要讓一切靜止。 你不需要模52位置:

int card = random1.nextInt (52); 

輸出:

Column 0   Column 1   Column 2 
====================================================== 
    18    7    2   
    47    11    34   
    0    45    26   
    10    9    41   
    35    31    23   
    16    22    39   
    4    8    50  

注意,我重命名的文件和類CardShuffle,所以它不會覆蓋其他2000 testing.java-文件。 如果printCard將返回一個字符串,您可以從deal打印它。

+0

只是在第二個for循環中添加card ++會做同樣的事情還是比做這個更好?如果我這樣做,它會在我的更大程序中工作嗎?非常感謝你的幫助,我真的很感激。 – 2012-03-28 11:40:50

+0

我將如何去打印printcard()返回一個字符串?我的主要目標是每次調用deal()時都會獲得3個隨機列,這樣我就可以讓用戶選擇他們的卡所在的列。這僅僅是我第二次使用數組,並且對我來說很難包裝我的頭:/ – 2012-03-28 11:48:08

+0

這取決於你想要什麼。我做了一個錯誤的猜測。它每次重新排列卡的子集。如果你不需要/想要的話,卡片++就沒問題。 '公共字符串printCard(int card){'(...)'返回隊列[rank] +「of + suit [suit];' – 2012-03-28 11:50:06

3

在第63行你寫play[row][col]=deck[card];但你永遠不會改變/增加可變card,所以你總是從獲取第一carddeck,因爲card始終爲0

+0

如何將「卡」更改爲「BuildDeck()」始終分配給「卡」的相同值?我假設'BuildDeck()'爲循環中的'card'生成一個新的值,因爲當我將'printcard()'放入其中時,會打印一張卡片中所有卡片的列表。並感謝您的答覆。 – 2012-03-28 11:15:39

+0

你可以改變'Deal'方法來接受另一個'int',它是你的起始卡。然後你在你的循環中增加這個變量,並在最後返回改變的變量。通過這種方式,您還可以獲得已經處理的卡片數量,並可以從此處繼續。 – Neet 2012-03-28 11:19:14

+1

你可以在'play [row] [col] = deck [card];'這行下面加'card ++',這會增加卡片變量,以便在下一次迭代中選取下一張卡片。 – Christoffer 2012-03-28 11:20:59

1

你有方法PrintCard()以可讀的格式輸出卡片,但是你根本不在程序中調用這個方法。

+0

我無法弄清楚如何使用它,它應該用在我正在建設的更大的程序中,我只是想弄清楚如何讓它打印出3排7張牌。 – 2012-03-28 11:17:02

+1

@Brett如果使用java和「更大的程序」更好地創建Card對象並覆蓋它的toString()方法。 – mishadoff 2012-03-28 11:24:45

+0

這裏是「更大的程序」,我必須按照它從這裏開始的方式完成程序:/儘管它看起來非常低效,但它是我被允許執行的唯一方法。 http://pastebin.com/99tLpVjM – 2012-03-28 11:30:45