Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
String[][] table = new String[][] { {"Spade","Diamond","Heart","Club"}, {"Ace", "2","3","4","5","6","7","8","9","Jack","Queen","King",} };
      
    String LuckyCard;
     for (int r = 0; r < table.length; r++)
    {
        for (int c = 0; c < table[r].length; c++)
            {
            int cardLucky = (int)(Math.random()*13) + 1; // generating random card

int suitLucky = (int)(Math.random()*4) + 1; // generating random suit

        
LuckyCard = "Lucky Card is " + table[cardLucky][suitLucky];
                    System.out.println(LuckyCard);
                  break;
        }}
   for(int i = 0; i < table.length; i++) {
            for(int j = 0; j < table[i].length; j++) {
                System.out.print(table[i][j] + " ");
            }
        }

}


What I have tried:

I understand that this part of my code "LuckyCard = "Lucky Card is " + table[cardLucky][suitLucky];" isn't working because my array is a string and tha math.random is for integers. Is there another way to do this to pick a random card from the 2d array?
Posted
Updated 14-Dec-17 16:18pm
v2

In this case you should not use a 2d array, but just two arrays:
C#
String[] suits = {"Spades","Diamonds","Hearts","Clubs"};
String[] cards = {"Ace", "2","3","4","5","6","7","8","9","Jack","Queen","King"};

int cardLucky = (int)(Math.random() * 13);
int suitLucky = (int)(Math.random() * 4);

LuckyCard = "Lucky Card is " + cards[cardLucky] + " of " + suits[suitLucky] + ".";
 
Share this answer
 
Comments
Member 13576973 14-Dec-17 21:29pm    
The teacher required us to use a 2d array
[no name] 14-Dec-17 22:18pm    
In that case you could create a 2d cards[4][13] array that you fill with (do it in a loop):

String[][] table = new String[][] { { "Spade Ace", "Spade 2", ...}, { "Diamond Ace", "Diamond 2", ...}, { "Heart Ace", "Heart 2", ...}, { "Club Ace", "Club 2", ...}};

and select a card with:

LuckyCard = "Lucky Card is " + table[suitLucky][cardLucky];

But this just isn't the best way to do it...
Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spoting structures mistakes.
Java
  String[][] table = new String[][] { {"Spade","Diamond","Heart","Club"}, {"Ace", "2","3","4","5","6","7","8","9","Jack","Queen","King",} };
  
  String LuckyCard;
  for (int r = 0; r < table.length; r++)
  {
    for (int c = 0; c < table[r].length; c++)
    {
      int cardLucky = (int)(Math.random()*13) + 1; // generating random card
  
      int suitLucky = (int)(Math.random()*4) + 1; // generating random suit
  
  
      LuckyCard = "Lucky Card is " + table[cardLucky][suitLucky];
      System.out.println(LuckyCard);
      break;
    }
  }
  for(int i = 0; i < table.length; i++) {
    for(int j = 0; j < table[i].length; j++) {
      System.out.print(table[i][j] + " ");
    }
  }

}

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]

This code is non sense, you need to explain what you try to do.
table is not a 2d array of cards, it is a list of 2 lists, the first one is a list of suits and the second is a list of cards values.
So this:
Java
LuckyCard = "Lucky Card is " + table[cardLucky][suitLucky];

must be replaced by:
Java
LuckyCard = "Lucky Card is " + table[1][cardLucky] + table[0][suitLucky];

your code have many other problems, but we need to know what you try to do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900