1

我想显示不同的字符串数组元素,如下所示:

  • 俱乐部 1
  • 红心王牌
  • 钻石 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();
    }
}

我将如何做?指出我显示这些不同元素的正确逻辑。谢谢。

4

6 回答 6

3

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

进入for循环,这样在每次迭代中都会生成一张新的随机卡片

于 2012-10-09T08:30:38.713 回答
2
    int times = 10; // Say you want it 10 times, in your code it's 7.

    for (int i = 0; i < times; i++) {
        System.out.println(deck[(int) (Math.random() * deck.length)]
                 + " : " + suit[(int) (Math.random() * suit.length)]);
    }
于 2012-10-09T08:34:55.757 回答
0
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();
    }
}
于 2012-10-09T08:32:55.810 回答
0
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);
    } 
} 

这将随机返回所有 52 张牌(所有组合)

于 2012-10-09T08:34:12.593 回答
0

我不太明白你的问题——如果你想要很多随机卡片,或者全部。

他们都是:

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

随机(给定数字):

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);
}
于 2012-10-09T08:34:39.600 回答
0
int i = (int) ( Math.random() * deck.length );
int j = (int) ( Math.random() * suit.length );

放入 for 循环,因为您的新生成 ij 值得到

于 2012-10-09T08:37:25.637 回答