Printing various array elements

I want to display various elements of a string array, for example:

  • Clubs 1
  • Hearts ace
  • Diamonds 9

CardGame.java:

public class CardGame { public static void main(String[] args){ String[] suit = { "Clubs", "Hearts", "Diamonds", "Spades" }; String[] deck = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Aces" }; int i = (int) ( Math.random() * deck.length ); int j = (int) ( Math.random() * suit.length ); for( int a = 0; a < 7; a++ ) { System.out.println( "Deck " + deck[i] + " Suit " + suit[j] ); } System.out.println(); } } 

How can I do it? Guide me to correct the display logic of these different elements. Thanks.

+1
source share
6 answers
  int times = 10; // Say you want it 10 times, in your code it 7. for (int i = 0; i < times; i++) { System.out.println(deck[(int) (Math.random() * deck.length)] + " : " + suit[(int) (Math.random() * suit.length)]); } 
+3
source

put

 int i = (int) ( Math.random() * deck.length ); int j = (int) ( Math.random() * suit.length ); 

into the for loop, so that at each iteration a new random map is generated

+4
source
 public class CardGame { public static void main(String[] args) { String[] deck = { "Clubs", "Hearts", "Diamonds", "Spades" }; String[] suit = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Aces" }; for( int a = 0; a < 7; a++ ) { int i = (int) ( Math.random() * deck.length ); int j = (int) ( Math.random() * suit.length ); System.out.println( "Deck " + deck[i] + " Suit " + suit[j] ); } System.out.println(); } } 
+1
source

I do not quite understand your question - if you want a lot of random cards or all of them.

All of them:

 for (String cardSuit: suit) { for (String cardDeck: deck) { System.out.println(cardDeck + " " + cardSuit); } } 

Random (set number):

 Random rnd = new java.util.Random(); for (int i = 0; i < number; i++) { String cardSuit = suit[rnd.nextInt(suit.length)], cardDeck = deck[rnd.nextInt(deck.length)]; System.out.println(cardDeck + " " + cardSuit); } 
+1
source
 int i = (int) ( Math.random() * deck.length ); int j = (int) ( Math.random() * suit.length ); 

put in a for loop because your new generation value i j gets

+1
source
 import java.util.HashSet; import java.util.Set; public class CardGame { public static void main(String[] args){ String[] deck = { "Clubs", "Hearts", "Diamonds", "Spades" }; String[] suit = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Aces" }; Set<String> s = new HashSet<String>(); int totalCards=0; while(s.size() < (deck.length * suit.length)){ int i = (int) ( Math.random() * deck.length ); int j = (int) ( Math.random() * suit.length ); if(!s.contains("Deck " + deck[i] + " Suit " + suit[j])){ s.add("Deck " + deck[i] + " Suit " + suit[j]); totalCards++; } } System.out.println("Cards at Random: "); for(String str: s) System.out.println(str); System.out.println("Total Cards: "+ totalCards); } } 

This will return all 52 cards (all combinations) randomly.

0
source

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


All Articles