Java map program

I am writing a program that does a card trick, but first I have to figure out how to play 3 rows of 7 cards. I can’t understand why, when I run my program, it displays only one number for all my cards and how I can print its name. I can get it to print 52 unique cards with the PrintCard () function, but not when I use Deal (), and I don't know why any help is appreciated. Thanks.

This is my code:

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 (); } } } 

great program: http://pastebin.com/99tLpVjM

+3
source share
3 answers

In a transaction, you never do anything with a card. Make it an iteration.

 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 (); } } 

So declare random at the class level:

 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); } 

Do not make everything static. You don't need modulo 52 here:

  int card = random1.nextInt (52); 

Conclusion:

  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 

Note that I renamed the file and class to CardShuffle, so it does not override the other 2000 test.java-Files. If printCard returns a string, you can print it from deal .

0
source

On line 63, you wrote play[row][col]=deck[card]; , but you never change / increase the variable card , so you always select the first card from your deck , because card always 0 .

+3
source

You have the PrintCard() method to output the card in a human-readable format, but you do not call this method in your program at all.

+1
source

Source: https://habr.com/ru/post/1403869/


All Articles