打印不同的数组元素 [英] Print different array elements
本文介绍了打印不同的数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要显示这样的不同的字符串数组元素:
- 俱乐部1
- 心王牌
- 钻石9
CardGame.java:
公共类CardGame {
公共静态无效的主要(字串[] args){
的String [] =套装{俱乐部,心,钻石,黑桃};
串[]甲板= {2,3,4,5,6,7,8,9,10,
杰克,皇后,王,王牌}; INT I =(INT)(的Math.random()* deck.length);
INT J =(INT)(的Math.random()* suit.length);
为(int类型的= 0;一7;;一个++){
的System.out.println(甲板+甲板[I] +套装+套装[J]);
} 的System.out.println();
}
}
我会怎么做呢?点我纠正这些显示不同元素的逻辑。谢谢你。
解决方案
INT次= 10; //你说你想要它的10倍,在你的code这是7。 的for(int i = 0; I<次;我++){
的System.out.println(甲板[(INT)(的Math.random()* deck.length)
+:+套装[(int)的(的Math.random()* suit.length)]);
}
I want to display different string array elements like this:
- 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 will I do it? Point me to correct logic of displaying those different elements. Thanks.
解决方案
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)]);
}
这篇关于打印不同的数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文