2012-03-28 67 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