A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© hzhzhen 中级黑马   /  2015-5-23 10:54  /  253 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.util.ArrayList;
  2. import java.util.Comparator;
  3. import java.util.Iterator;
  4. import java.util.Random;
  5. import java.util.TreeSet;

  6. public class px {
  7. Random rd;
  8. TreeSet ts;
  9. TreeSet ts1;
  10. TreeSet ts2;
  11. TreeSet ts3;
  12. TreeSet ts4;
  13. px(){
  14.   rd=new Random();
  15.   //ts=new TreeSet();
  16.   
  17.   ts=new TreeSet(new Comparator(){

  18.    public int compare(Object o1, Object o2) {
  19.     player p1=(player)o1;
  20.     player p2=(player)o2;
  21.     return p1.whole<p2.whole?1:p1.whole<p2.whole?-1:0;
  22.    }
  23.    
  24.   });
  25.   
  26.   ts1=new TreeSet(new Comparator(){

  27.    public int compare(Object o1, Object o2) {
  28.     player p1=(player)o1;
  29.     player p2=(player)o2;
  30.     return p1.money<p2.money?1:p1.money<p2.money?-1:0;
  31.    }
  32.    
  33.   });
  34.   
  35.   ts2=new TreeSet(new Comparator(){

  36.    public int compare(Object o1, Object o2) {
  37.     player p1=(player)o1;
  38.     player p2=(player)o2;
  39.     return p1.wood<p2.wood?1:p1.wood<p2.wood?-1:0;
  40.    }
  41.    
  42.   });
  43.   
  44.   ts3=new TreeSet(new Comparator(){

  45.    public int compare(Object o1, Object o2) {
  46.     player p1=(player)o1;
  47.     player p2=(player)o2;
  48.     return p1.sin<p2.sin?1:p1.sin<p2.sin?-1:0;
  49.    }
  50.    
  51.   });
  52.   
  53.   ts4=new TreeSet(new Comparator(){

  54.    public int compare(Object o1, Object o2) {
  55.     player p1=(player)o1;
  56.     player p2=(player)o2;
  57.     return p1.death<p2.death?1:p1.death<p2.death?-1:0;
  58.    }
  59.    
  60.   });
  61. }

  62. public void generate(){
  63.   for(int i=0;i<5;i++){
  64.    player p=new player(rd.nextInt(100),rd.nextInt(100),rd.nextInt(100),rd.nextInt(100));
  65.    add(p);
  66.   }
  67. }
  68. public void add(player p){
  69.   ts.add(p);
  70.   ts1.add(p);
  71.   ts2.add(p);
  72.   ts3.add(p);
  73.   ts4.add(p);
  74. }
  75. public void show(TreeSet ts){
  76.   System.out.println(ts.size());
  77.   Iterator it=ts.iterator();
  78.   while(it.hasNext()){
  79.    ((player)it.next()).show();
  80.   }
  81.   System.out.println();
  82. }
  83. public static void main(String[] args) {
  84.   px p=new px();
  85.   p.generate();
  86.   p.show(p.ts);
  87. }
  88. }
  89. class player/* implements Comparator*/{
  90. int money;
  91. int wood;
  92. int sin;
  93. int death;
  94. int whole;
  95. player(int money,int wood,int sin,int death){
  96.   this.money=money;
  97.   this.wood=wood;
  98.   this.sin=sin;
  99.   this.death=death;
  100.   whole=money+wood+sin+death;
  101. }
  102. public void show(){
  103.   System.out.print("金币:"+money+'\t');
  104.   System.out.print("木材:"+wood+'\t');
  105.   System.out.print("罪恶值:"+sin+'\t');
  106.   System.out.print("死亡数:"+death+'\t');
  107.   System.out.println("总分:"+whole);
  108. }
  109. /*public int compare(Object o1, Object o2) {
  110.   player p1=(player)o1;
  111.   player p2=(player)o2;
  112.   return p1.whole<p2.whole?1:p1.whole<p2.whole?-1:0;
  113. }*/
  114. }
复制代码

1 个回复

倒序浏览
比较规则有问题把“ p1.whole<p2.whole?1:p1.whole<p2.whole?-1:0;”中的第二个“<”改为>。类似的都修改。下面是我改过的,5个Player都有,你试试:
  1. import java.util.ArrayList;
  2. import java.util.Comparator;
  3. import java.util.Iterator;
  4. import java.util.Random;
  5. import java.util.TreeSet;

  6. public class PX {
  7.     Random rd;
  8.     TreeSet ts;
  9.     TreeSet ts1;
  10.     TreeSet ts2;
  11.     TreeSet ts3;
  12.     TreeSet ts4;
  13.     PX(){
  14.          rd=new Random();
  15.          //ts=new TreeSet();
  16.          
  17.          ts=new TreeSet(new Comparator(){
  18.               public int compare(Object o1, Object o2) {
  19.                    player p1=(player)o1;
  20.                    player p2=(player)o2;
  21.                    return p1.whole<p2.whole?1:p1.whole>p2.whole?-1:0;
  22.               }
  23.          });
  24.          ts1=new TreeSet(new Comparator(){
  25.               public int compare(Object o1, Object o2) {
  26.                    player p1=(player)o1;
  27.                    player p2=(player)o2;
  28.                    return p1.money<p2.money?1:p1.money>p2.money?-1:0;
  29.               }
  30.          });
  31.          ts2=new TreeSet(new Comparator(){
  32.               public int compare(Object o1, Object o2) {
  33.                    player p1=(player)o1;
  34.                    player p2=(player)o2;
  35.                    return p1.wood<p2.wood?1:p1.wood>p2.wood?-1:0;
  36.               }
  37.          });
  38.          ts3=new TreeSet(new Comparator(){
  39.               public int compare(Object o1, Object o2) {
  40.                    player p1=(player)o1;
  41.                    player p2=(player)o2;
  42.                    return p1.sin<p2.sin?1:p1.sin>p2.sin?-1:0;
  43.               }
  44.          });
  45.          ts4=new TreeSet(new Comparator(){
  46.               public int compare(Object o1, Object o2) {
  47.                    player p1=(player)o1;
  48.                    player p2=(player)o2;
  49.                    return p1.death<p2.death?1:p1.death>p2.death?-1:0;
  50.               }
  51.          });
  52.     }
  53.     public void generate(){
  54.          for(int i=0;i<5;i++){
  55.               player p=new player(rd.nextInt(100),rd.nextInt(100),rd.nextInt(100),rd.nextInt(100));
  56.               add(p);
  57.          }
  58.     }
  59.     public void add(player p){
  60.          ts.add(p);
  61.          ts1.add(p);
  62.          ts2.add(p);
  63.          ts3.add(p);
  64.          ts4.add(p);
  65.     }
  66.     public void show(TreeSet ts){
  67.          System.out.println(ts.size());
  68.          Iterator it=ts.iterator();
  69.          while(it.hasNext()){
  70.               ((player)it.next()).show();
  71.          }
  72.          System.out.println();
  73.     }
  74.     public static void main(String[] args) {
  75.          PX p=new PX();
  76.          p.generate();
  77.          p.show(p.ts);
  78.          p.show(p.ts1);
  79.          p.show(p.ts2);
  80.          p.show(p.ts3);
  81.          p.show(p.ts4);
  82.     }
  83. }
  84. class player/* implements Comparator*/{
  85.      int money;
  86.      int wood;
  87.      int sin;
  88.      int death;
  89.      int whole;
  90.      player(int money,int wood,int sin,int death){
  91.           this.money=money;
  92.           this.wood=wood;
  93.           this.sin=sin;
  94.           this.death=death;
  95.           whole=money+wood+sin+death;
  96.      }
  97.      public void show(){
  98.           System.out.print("金币:"+money+'\t');
  99.           System.out.print("木材:"+wood+'\t');
  100.           System.out.print("罪恶值:"+sin+'\t');
  101.           System.out.print("死亡数:"+death+'\t');
  102.           System.out.println("总分:"+whole);
  103.      }
  104. /*public int compare(Object o1, Object o2) {
  105.   player p1=(player)o1;
  106.   player p2=(player)o2;
  107.   return p1.whole<p2.whole?1:p1.whole<p2.whole?-1:0;
  108. }*/
  109. }
复制代码



回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马