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

我知道代码有点多大家都不想细看!!但是我真的不会搞排序那里!!求帮助 !!

我的程序问题就是 排序的时候汉字排序不是按照要求的 黑桃然后红桃等等这样子,还有扑克牌编号的数字值和字母值要怎么比较?

比如说 2 和 J 。 不知道怎么重写equals方法!求指导!

 

TIPS:下面是我所有的代码 ,第一个是 CardList 类,所有主程序都在里面 ,第二个是 Card类,第三个是 People 类

 

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

 

public class CardsList {
 
 public List<Card> cardsList;
 
 public CardsList(){
  cardsList=new ArrayList<Card>();
 }
 
 public void testAddAll(){
  Card[] cards={new Card("黑桃","A"),new Card("黑桃","2"),new Card("黑桃","3"),new Card("黑桃","4"),new Card("黑桃","5"),new Card("黑桃","6"),new Card("黑桃","7"),new Card("黑桃","8"),new Card("黑桃","9"),new Card("黑桃","10"),new Card("黑桃","J"),new Card("黑桃","Q"),new Card("黑桃","K"),
       new Card("红桃","A"),new Card("红桃","2"),new Card("红桃","3"),new Card("红桃","4"),new Card("红桃","5"),new Card("红桃","6"),new Card("红桃","7"),new Card("红桃","8"),new Card("红桃","9"),new Card("红桃","10"),new Card("红桃","J"),new Card("红桃","Q"),new Card("红桃","K"),
       new Card("梅花","A"),new Card("梅花","2"),new Card("梅花","3"),new Card("梅花","4"),new Card("梅花","5"),new Card("梅花","6"),new Card("梅花","7"),new Card("梅花","8"),new Card("梅花","9"),new Card("梅花","10"),new Card("梅花","J"),new Card("梅花","Q"),new Card("梅花","K"),
       new Card("方块","A"),new Card("方块","2"),new Card("方块","3"),new Card("方块","4"),new Card("方块","5"),new Card("方块","6"),new Card("方块","7"),new Card("方块","8"),new Card("方块","9"),new Card("方块","10"),new Card("方块","J"),new Card("方块","Q"),new Card("方块","K")};
  cardsList.addAll(Arrays.asList(cards));
  System.out.println("扑克牌已经准备好啦 ,可以开始游戏了!");
 
 }
 
 public void testForEach(){
  System.out.println("扑克牌显示:");
  for(Card cd:cardsList){
   System.out.print("扑克牌:"+cd.color+" "+cd.number+" ");
  }
  System.out.println(" ");
 }

 public void testChangeTheList(){
  System.out.println("这是打乱顺序的纸牌了!!");
  Collections.shuffle(cardsList);
 }
 
 public void testpeopleGetCard(){
  Random random=new Random();
  Scanner console=new Scanner(System.in);
  int k,k2,k3,k4;
  do{
   k=random.nextInt(51);
   k2=random.nextInt(51);
   k3=random.nextInt(51);
   k4=random.nextInt(51);
  }while(k==k2&&k==k3&&k==k4&&k2==k3&&k2==k4&&k3==k4);
 
  System.out.println("请输入所要创建角色ID:");
  String ID=console.next();
  System.out.println("请输入所要创建角色Name:");
  String name=console.next();
  Card[] Acards={(Card)cardsList.get(k),(Card)cardsList.get(k2)};
  People people1=new People(ID,name,Acards);
 
  System.out.println("请输入所要创建角色ID:");
  String ID2=console.next();
  System.out.println("请输入所要创建角色Name:");
  String name2=console.next(); 
  Card[] Bcards={(Card)cardsList.get(k3),(Card)cardsList.get(k4)};
  People people2=new People(ID2,name2,Bcards);
 
 
 
 
  System.out.println("角色创建成功!"+people1.name+" "+people2.name);
  System.out.println("发牌成功!!");
  System.out.println("玩家:"+people1.name+" "+Acards[0].color+" "+Acards[0].number+" "+Acards[1].color+" "+Acards[1].number);
  System.out.println("玩家:"+people2.name+" "+Bcards[0].color+" "+Bcards[0].number+" "+Bcards[1].color+" "+Bcards[1].number);
  chooseTheBigger(Acards);
  System.out.println("玩家1最大的牌为:"+Acards[1].color+Acards[1].number);
 
  chooseTheBigger(Bcards);
  System.out.println("玩家2最大的牌为:"+Bcards[1].color+Bcards[1].number);
 
  Card[] cards={Acards[1],Bcards[1]};
  chooseTheBigger(cards);
  System.out.println("最大的扑克牌为:"+cards[1].color+cards[1].number);
  if(cards[1].equals(Acards[1])){
   System.out.println("玩家"+people1.name+"获胜了!");
  }else{
   System.out.println("玩家"+people2.name+"获胜了!");
  }
 
 }
 
 public void chooseTheBigger(Card[] cards){
  List<Card> test=new ArrayList();
  test.addAll(Arrays.asList(cards));
  Collections.sort(test);
  cards[1]=test.get(0);
  for (Card card : test) {
   System.out.println("扑克牌:"+card.color+card.number);
  }
       
 }
 
 public static void main(String[] args) {
  // TODO Auto-generated method stub
        CardsList cl=new CardsList();
        cl.testAddAll();
        cl.testForEach();
        cl.testChangeTheList();
        cl.testForEach();
        cl.testpeopleGetCard();
 }

}

 

public class Card implements Comparable<Card>{
 
 public String color;
 public String number;
 public Card(String color,String number){
  this.color=color;
  this.number=number;
 }
 @Override
 public int compareTo(Card arg0) {
  // TODO Auto-generated method stub
  if(!this.color.equals(arg0.color)){
   return this.color.compareTo(arg0.color);
  }else{
   return this.number.compareTo(arg0.number);
  }
 }
 @Override
 public int hashCode() {
  final int prime = 31;
  int result = 1;
  result = prime * result + ((color == null) ? 0 : color.hashCode());
  result = prime * result + ((number == null) ? 0 : number.hashCode());
  return result;
 }
 @Override
 public boolean equals(Object obj) {
  if (this == obj)
   return true;
  if (obj == null)
   return false;
  if (!(obj instanceof Card))
   return false;
  Card other = (Card) obj;
  if (color == null) {
   if (other.color != null)
    return false;
  } else if (!color.equals(other.color))
   return false;
  if (number == null) {
   if (other.number != null)
    return false;
  } else if (!number.equals(other.number))
   return false;
  return true;
 }
}

 

 

 


public class People {
    public String id;
    public String name;
    public Card[] cards;
    public People(String id,String name,Card[] cards){
     this.id=id;
     this.name=name;
     this.cards=cards;
    }
}


正在回答

1 回答

问题已经解决了,根据正确顺序创建两个字符串,通过调用indexOf的方法进行比较大小。

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

举报

0/150
提交
取消

我知道代码有点多大家都不想细看!!但是我真的不会搞排序那里!!求帮助 !!

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