亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

為什么比較大小結果不理想 不管什么都是平局。

public int compareTo(Pukepai arg0) {

if(this.getcardPoints()==arg0.getcardPoints()){

return this.getCardSuit().compareTo(cardSuit);

}

else{

return this.getCardPoints().compareTo(cardPoints);

}

}

? ??

}

public class Game {

? private List<Play> playList;

? private List<Pukepai> pukepaiList;

? public Game(){

?playList=new ArrayList<Play>();

?pukepaiList=new ArrayList<Pukepai>();

? }

? //初始化牌(創建牌)

? public void Chuangjianpai(){

?System.out.println("-----生成撲克牌-----");

?String[] cardSuits={"黑桃","紅桃","梅花","方片"};

?String[] cardPoints={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};

?for(int i=0;i<cardSuits.length;i++){

?for(int j=0;j<cardPoints.length;j++){

?pukepaiList.add(new Pukepai(cardPoints[j],cardSuits[i]));

?}

?}

?System.out.println("創建成功");

?System.out.println(pukepaiList);

? }

? //洗牌

? public void Xipai(){

?System.out.println("---開始洗牌----");

?Collections.shuffle(pukepaiList);

?System.out.println("---洗牌結束----");

? }

? //創建玩家

? public void ChuangjianPlayer(){

?Scanner ID=new Scanner(System.in);

?System.out.println("請輸入第一位玩家ID");

?String id1=ID.next();

?System.out.println("請輸入第二位玩家ID");

?String id2 =ID.next();

?Scanner NAME =new Scanner(System.in);

?System.out.println("請輸入第一位玩家NAME");

?String name1=NAME.next();

?System.out.println("請輸入第二位玩家NAME");

?String name2=NAME.next();

?System.out.println("成功添加玩家一:"+id1+name1);

?System.out.println("成功添加玩家二:"+id2+name2);

?playList.add(new Play(name1, id1));

?playList.add(new Play(name2, id2));

? }

? //發牌

? public void Fapai(){

?System.out.println("---開始發牌----");

?for(int i=0;i<2;i++){

?for(int j=0;j<playList.size();j++){

?System.out.println("玩家:"+playList.get(j).getName()+"拿牌");

?playList.get(j).setPukepai(pukepaiList.get(0));

?pukepaiList.remove(0);

?}

?}

?System.out.println("---發牌結束----");

? }

? //比大小(游戲開始)

? public void StartGame(){

System.out.println("---游戲開始----");

for(int i=0;i<playList.size();i++){

Collections.sort(playList.get(i).pukepaiList);

System.out.println("玩家"+playList.get(i).getName()+"最大手牌為:"+

playList.get(i).pukepaiList.get(1));

}

//接比較結果放于daxiao變量

int daxiao=playList.get(0).pukepaiList.get(1).compareTo(playList.get(1).pukepaiList.get(1));

System.out.println(daxiao);

if(daxiao>0){

String name1=playList.get(0).getName();

System.out.println("玩家"+name1+"獲勝");?

}

else if(daxiao<0){

String name2=playList.get(1).getName();

System.out.println("玩家"+name2+"獲勝");

}else{

System.out.println("平局");

}

? }

https://img1.sycdn.imooc.com//5b6121240001003b04320314.jpg

正在回答

1 回答

請問你的問題解決了嗎?是不是compareTo 方法的問題啊?能把你的 pukepai 類的代碼貼出來看一下嗎?想參考參考

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

為什么比較大小結果不理想 不管什么都是平局。

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號