java實現玩牌

public class Card {
public static void main(String[] args) {
int[] cards = new int[52];
String[] suits = {"紅桃","黑桃","方塊","梅花"};
String[] ranks = {"A","2","3","4","5","6","7"
,"8","9","10","J","Q","K"};
// 52張牌賦初值
for (int i = 0; i < cards.length; i++) {
cards[i] = i ;
}
int[] index = new int[4];
// 生成四個隨機數
for (int i = 0; i < 4; i++) {
index[i] = (int) (Math.random() * 51) + 1;
// 輸出四張牌的內容
System.out.print(index[i] + " ");
// 將具體的數值轉化成相應的牌的花色
String suit = suits[index[i] / 13];
/*if (cards[index[i]] / 13 == 0) {
System.out.print("紅桃");
} else if (cards[index[i]] / 13 == 1) {
System.out.print("黑桃");
} else if (cards[index[i]] / 13 == 2) {
System.out.print("方塊");
} else if (cards[index[i]] / 13 == 3) {
System.out.print("梅花");
}*/
//計算牌值cards[index[i]] % 13
String rank = ranks[index[i]%13];
/*if(cards[index[i]] % 13==1){
System.out.print("A");
}else if(cards[index[i]] % 13==11){
System.out.print("J");
}else if(cards[index[i]] % 13==12){
System.out.print("Q");
}else if(cards[index[i]] % 13==0){
System.out.print("K");
}else{
System.out.print(cards[index[i]]%13);
}*/
System.out.print(suit + rank + " ");

}
System.out.println();


}


}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章