java 斗地主的写法


package com.yunhe.code;


import java.util.Random;

public class Array {
private static int[] fun(){ //5,6,18,
int [] res = new int[54];
int k =0 ; //用来标识数组中已经有的元素的个数
for(int i=0;iint digit = new Random().nextInt(54);//[0,54)//产生随机数
if(k==0){
res[i] = digit;
k++; 
}else{
if(exists(res, k, digit)){//存在,继续产生下一个
i--;
continue;
}else{
res[i]=digit;
k++;
}
}
}
return res;
}
//Key是否位于数组array的前length个元素中
private static boolean exists(int[] array ,int length,int key){
for(int i=0;iif(key == array[i]){
return true;
}
}
return false;
}

public static void main(String[] args) {
int[] dd = fun();


int k=0;
String[] source = {"1♠","2♠","3♠","4♠","5♠","6♠","7♠","8♠","9♠","10♠","11♠","12♠","13♠",
"1♦","2♦","3♦","4♦","5♦","6♦","7♦","8♦","9♦","10♦","11♦","12♦","13♦",
"1♥","2♥","3♥","4♥","5♥","6♥","7♥","8♥","9♥","10♥","11♥","12♥","13♥",
"1♣","2♣","3♣","4♣","5♣","6♣","7♣","8♣","9♣","10♣","11♣","12♣","13♣",
"king","queen"};
String[][] data = new String[3][17];
for(int i =0;ifor(int j=0;jdata[i][j]= source[dd[k++]];
}
}

for(int i =0;ifor(int j=0;jSystem.out.print(data[i][j]+"\t");
}
System.out.println();
}
}

}

云和学院 毛孬 Q285673392

你可能感兴趣的:(基础习题)