我正在尝试使用 Java 数组。我想要做的是计算数组字符串元素的出现次数。例如:
Clubs - 8
Clubs - Ace
Clubs - Jack
Hearts - 9
Spades - 3
Hearts - 6
出现次数:
Clubs - 3
Hearts - 2
Spades - 1
Diamonds - 0
到目前为止,我只编写了这个:
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" };
for( int a = 0; a < 7; a++ ){
int i = (int) ( Math.random() * suit.length );
int j = (int) ( Math.random() * deck.length );
//System.out.println( "Suit " + suit[i] + " Deck " + deck[j] );
System.out.println( suit[(int) (Math.random() * suit.length)]
+ " : " + deck[(int) (Math.random() * deck.length)]);
}
System.out.println();
}
}
给我一些关于如何做到这一点的想法。谢谢。
注意:这不是家庭作业。我只是想给自己一些关于数组的练习。