java数组按照大小排列_Java怎么让数组中元素按照牌值从小到大的顺序排列
展開全部
你說的這些牌是用數組來存放的,所以有些麻煩。直接復制運行,更改牌的62616964757a686964616fe58685e5aeb931333332643838數量測試,有不懂的再問我package?test;
import?java.util.Arrays;
import?java.util.Random;
public?class?DaoTest?{
public?static?void?main(String[]?args)?{
String[]?str?=?getArr(100);
str?=?sorts(str);
System.out.println(Arrays.toString(str));
}
/**
*?從牌堆里抽出n張牌,可以有重復
*
*?@param?n
*?@return
*/
public?static?String[]?getArr(int?n)?{
String[]?suit?=?{?"黑桃",?"紅心",?"梅花",?"方塊"?};
String[]?number?=?{?"A",?"2",?"3",?"4",?"5",?"6",?"7",?"8",?"9",?"10",
"J",?"Q",?"K"?};
String[]?result?=?new?String[54];
int?index?=?0;
for?(int?i?=?0;?i?
for?(int?j?=?0;?j?
result[index]?=?""?+?suit[i]?+?number[j];
index++;
}
}
result[52]?=?"小王";
result[53]?=?"大王";
String[]?some?=?new?String[n];
for?(int?i?=?0;?i?
some[i]?=?result[(new?Random()).nextInt(54)];
}
return?some;
}
/**
*?將所有牌分為5組,排序后在放到一起
*?@param?arr
*?@return
*/
public?static?String[]?sorts(String[]?arr)?{
System.out.println(Arrays.toString(arr));
StringBuffer?ht?=?new?StringBuffer("");
StringBuffer?hx?=?new?StringBuffer("");
StringBuffer?mh?=?new?StringBuffer("");
StringBuffer?fk?=?new?StringBuffer("");
StringBuffer?king?=?new?StringBuffer();
for?(int?i?=?0;?i?
if?(arr[i].length()?==?2)?{
if?(king.toString().isEmpty())?{
king.append(arr[i]);
}?else?{
king.append(","?+?arr[i]);
}
}?else?if?(arr[i].contains("黑桃"))?{
if?(ht.toString().isEmpty())?{
ht.append(arr[i]);
}else{
ht.append(","?+?arr[i]);
}
}?else?if?(arr[i].contains("紅心"))?{
if?(hx.toString().isEmpty())?{
hx.append(arr[i]);
}else{
hx.append(","?+?arr[i]);
}
}?else?if?(arr[i].contains("梅花"))?{
if?(mh.toString().isEmpty())?{
mh.append(arr[i]);
}else{
mh.append(","?+?arr[i]);
}
}?else?{
if?(fk.toString().isEmpty())?{
fk.append(arr[i]);
}else{
fk.append(","?+?arr[i]);
}
}
}
String[]?aht?=?ht.toString().trim().split(",");
String[]?ahx?=?hx.toString().trim().split(",");
String[]?amh?=?mh.toString().trim().split(",");
String[]?afk?=?fk.toString().trim().split(",");
String[]?aking?=?king.toString().trim().split(",");
aht?=?sort1(aht);
ahx?=?sort1(ahx);
amh?=?sort1(amh);
afk?=?sort1(afk);
aking?=?sort1(aking);
String[]?result?=?new?String[arr.length];
int?index?=?0;
if?(aht[0].length()?!=?0)?{
for?(int?i?=?0;?i?
result[index]?=?aht[i];
index++;
}
}
if?(ahx[0].length()?!=?0)?{
for?(int?i?=?0;?i?
result[index]?=?ahx[i];
index++;
}
}
if?(amh[0].length()?!=?0)?{
for?(int?i?=?0;?i?
result[index]?=?amh[i];
index++;
}
}
if?(afk[0].length()?!=?0)?{
for?(int?i?=?0;?i?
result[index]?=?afk[i];
index++;
}
}
if?(aking[0].length()?!=?0)?{
for?(int?i?=?0;?i?
result[index]?=?aking[i];
index++;
}
}
return?result;
}
/**
*?按照3-J-A-2-小王-大王的順序排序
*?@param?some
*?@return
*/
public?static?String[]?sort1(String[]?some)?{
if?(some.length?==?1?&&?some[0].length()?==?0)?{
return?some;
}
if?(some[0].length()?==?2)?{
Arrays.sort(some);
String[]?result?=?new?String[some.length];
for(int?i?=?0;i
result[i]?=?some[some.length-1-i];
}
return?result;
}
String[]?result?=?new?String[some.length];
int[]?number?=?new?int[some.length];
String?suit?=?""?+?some[0].charAt(0)?+?some[0].charAt(1);
for?(int?i?=?0;?i?
String?s?=?""?+?some[i].substring(2);
if?("A".equals(s))?{
s?=?"14";
}?else?if?("J".equals(s))?{
s?=?"11";
}?else?if?("Q".equals(s))?{
s?=?"12";
}?else?if?("K".equals(s))?{
s?=?"13";
}?else?if?("2".equals(s))?{
s?=?"15";
}
number[i]?=?Integer.parseInt(s);
}
Arrays.sort(number);
for?(int?i?=?0;?i?
String?s?=?""?+?number[i];
if?(number[i]?==?11)?{
s?=?"J";
}?else?if?(number[i]?==?12)?{
s?=?"Q";
}?else?if?(number[i]?==?13)?{
s?=?"K";
}?else?if?(number[i]?==?14)?{
s?=?"A";
}?else?if?(number[i]?==?15)?{
s?=?"2";
}
result[i]?=?suit?+?s;
}
return?result;
}
}
總結
以上是生活随笔為你收集整理的java数组按照大小排列_Java怎么让数组中元素按照牌值从小到大的顺序排列的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 百分制转化为五级制java_javav
- 下一篇: SD 存储卡的发展