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

大佬们,comparable方法哪里有问题 能指点一下下吗

package com.imooc;import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.List;import java.util.Scanner;public class Puke implements Comparable {public String color;public String num;public List<Puke>pukes;public List<Player>players; //玩家列表public Puke(String color,String num){	this.color=color;	this.num=num;	this.pukes=new ArrayList<Puke>();	this.players=new ArrayList<Player>();}public Puke(){	this.pukes=new ArrayList<Puke>();	this.players=new ArrayList<Player>();}/* *创建扑克牌 */public void creatPuke(){	String color[]={"黑桃","红桃","梅花","方片"};	String num[]={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};	for(int i=0;i<4;i++){		for(int j=0;j<13;j++){			Puke puke=new Puke(color[i],num[j]);			pukes.add(puke);		}	}}/* * 创建玩家 */public void creatPlayer(){	Scanner input=new Scanner(System.in);	for(int i=1;i<=2;i++){		System.out.println("请玩家输入ID:");		String id=input.next();		System.out.println("请玩家输入姓名:");		String name=input.next();		Player player=new Player(id,name);		players.add(player);	}	System.out.println("******一共有"+players.size()+"名玩家******");	for(Player player:players){		System.out.println("*********玩家:"+player.name+"**********");	}}/* * 洗牌 */public void rushPuke(){	System.out.println("******洗**牌******");	Collections.shuffle(pukes);}/* * 发牌 */public void sendPuke(){	System.out.println("******洗牌*结束******");	System.out.println("********发**牌********");	Puke a;	for(int i=0;i<4;i++){		a=pukes.get(i);		if(i%2==0){			players.get(0).cards.add(a);			System.out.println("*****玩家"+players.get(0).name+"拿牌*****");		}else{			players.get(1).cards.add(a);			System.out.println("*****玩家"+players.get(1).name+"拿牌*****");		}		}}/* * 比较大小 */public void comparable(){Puke p1=players.get(0).cards.get(0);Puke p2=players.get(0).cards.get(1);Puke p3=players.get(0).cards.get(0);Puke p4=players.get(0).cards.get(1);if((p1.compareTo(p3)>0)&&(p1.compareTo(p4)>0)){	System.out.println("游戏获胜者是玩家:"+players.get(0).name);}if((p2.compareTo(p3)>0)&&(p2.compareTo(p4)>0)){	System.out.println("游戏获胜者是玩家:"+players.get(0).name);}if((p3.compareTo(p1)>0)&&(p3.compareTo(p2)>0)){	System.out.println("游戏获胜者是玩家:"+players.get(1).name);}if((p3.compareTo(p1)>0)&&(p4.compareTo(p2)>0)){	System.out.println("游戏获胜者是玩家:"+players.get(1).name);}}public int compareTo(Puke P2) {		String colorOrder = "黑桃,红桃,梅花,方块";		String numOrder = "2,3,4,5,6,7,8,9,10,J,Q,K,A,";		int x = numOrder.indexOf(this.getNum());		int y = numOrder.indexOf(P2.getNum());		if(x<y) {		return -1;		}else if(x>y) {		return 1;		}else{		return 0;		}}public String getNum() {	// TODO Auto-generated method stub	return null;}/* * 遍历扑克 */public void forPuke(){	System.out.println("52张扑克如下");	for(Puke puke:pukes){		System.out.println(puke.color+":"+puke.num);	}}public static void main(String[] args) {	Puke pk=new Puke();	pk.creatPuke();	pk.forPuke();	pk.creatPlayer();	pk.rushPuke();	pk.sendPuke();	pk.comparable();}}


正在回答

1 回答

Puke类在实现可比较接口时要指明要比较的对象的类

Public class Puke implements Comparable<Puke>{}

然后调整CompareTo函数就行了

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

举报

0/150
提交
取消

大佬们,comparable方法哪里有问题 能指点一下下吗

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