为了账号安全,请及时绑定邮箱和手机立即绑定

重新整理了下,基本实现了功能,不过和老师的略有不同,加油!完成第一季到第三季

package poketest;
public class card implements Comparable<card>{//记得加泛型
    String huase;
    String num;
    Double huasevalue;
    int numvalue;
    Double value;
    public card(String huase,String num,Double huasevalue,int numvalue){
        this.huase=huase;
        this.num=num;
        this.value=numvalue*10+huasevalue;
    }
    @Override
    public int compareTo(card o) {
        return this.value.compareTo(o.value);
    }
}
package poketest;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class player {
    int id;
    String name;
    List<card> handcard;
    Scanner input;
    public player(){
        handcard=new ArrayList<>();
        System.out.println("请输入玩家的id");
        input=new Scanner(System.in);
        this.id=input.nextInt();
        System.out.println("玩家id为"+this.id+"");
        System.out.println("请输入玩家的姓名");
        this.name=input.next();
        System.out.println("玩家姓名为"+this.name+"");
    }
}
package poketest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class poke {
    //属性
    //List<card> suijipai;
    List<card> pai=new ArrayList<card>();//直接定义,为什么不能用构造方法
    player player1=new player();
    player player2=new player();
    card p1maxcard;
    card p2maxcard;

    //方法1 初始化完整的顺序牌
    public void intialCard(){
        String[] huase0={"黑桃","红桃","梅花","方片"};
        String[] num0={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
        double [] huasevalue0={1.0,2.0,3.0,4.0};
        int[] numvalue0={2,3,4,5,6,7,8,9,10,11,12,13,14};
        for(int i=0;i<4;i++){
            for(int j=0;j<13;j++){
                card c0=new card(huase0[i],num0[j],huasevalue0[i],numvalue0[j]);
                this.pai.add(c0);
            }
        }
        System.out.println("扑克初始化完成了啊");
    }
    public void show(){
        int index=0;
        System.out.println("开始展示扑克的" +
                "顺序啦啦啦");
        for (card c:this.pai) {
            System.out.print(c.huase+c.num+",");
            index++;
            if(index%13==0){
                System.out.println();
            }
        }System.out.println();
    }
    public void fapai(){
        this.player1.handcard.add(pai.get(0));
        System.out.println("玩家1摸第一张牌");
        this.player2.handcard.add(pai.get(1));
        System.out.println("玩家2摸第二张牌");
        this.player1.handcard.add(pai.get(2));
        System.out.println("玩家1摸第三张牌");
        this.player2.handcard.add(pai.get(3));
        System.out.println("玩家2摸第四张牌");
    }
    public void showMaxCard(){
        //比较player1两张牌哪个大并输出;比较player2两张牌哪个大并输出。
        Collections.sort(this.player1.handcard);
        Collections.sort(this.player2.handcard);
        p1maxcard=this.player1.handcard.get(1);
        p2maxcard=this.player2.handcard.get(1);
        System.out.println("玩家1手中最大的牌是"+p1maxcard.huase+p1maxcard.num);
        System.out.println("玩家2手中最大的牌是"+p2maxcard.huase+p2maxcard.num);
    }
    public void playerFight(){
        if(this.p1maxcard.value>this.p2maxcard.value){
            System.out.println("玩家一:"+this.player1.name+"胜利");
        }else{
            System.out.println("玩家二:"+this.player2.name+"胜利");
        }
    }
    public void showHandCard(){
        System.out.print("玩家一"+this.player1.name+"的手牌为");
        for (card showcard:this.player1.handcard) {
            System.out.print(showcard.huase+showcard.num+",");
        }
        System.out.println("---------------------------------");
        System.out.print("玩家二"+this.player2.name+"的手牌为");
        for (card showcard:this.player2.handcard) {
            System.out.print(showcard.huase+showcard.num+",");
        }
    }
}
package poketest;
import java.util.Collections;
public class mainmethod {
    public static void main(String[] args) {
        poke p1=new poke();
        p1.intialCard();
        p1.show();
        Collections.shuffle(p1.pai);
        System.out.println("洗牌!!!!!");
        p1.show();
        p1.fapai();
        p1.showMaxCard();
        p1.playerFight();
        p1.showHandCard();
    }
}

运行情况如下:

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


正在回答

2 回答

我想问一下我基本照着你的写的为啥会报错。就是 return this.rank.compareTO(o.rank);这一句。报错是

Cannot invoke compareTO(int) on the primitive type int,我查了查还是没搞懂错在哪了





package com.ft;

public class Card implements Comparable<Card>{
   String color;
   String point;
   int rank;
  public Card(String color,String point,int rank) {
      this.color=color;
      this.point=point;
      this.rank=rank;
  }
  //public Card(){
 // };
@Override
public int compareTo(Card o) {
    // TODO Auto-generated method stub
    return this.rank.compareTO(o.rank);
}
}

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

膜拜一下,准备开始写!

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

举报

0/150
提交
取消
Java入门第三季
  • 参与学习       409792    人
  • 解答问题       4340    个

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

进入课程

重新整理了下,基本实现了功能,不过和老师的略有不同,加油!完成第一季到第三季

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信