首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java CompareTo方法

Java CompareTo方法
EN

Stack Overflow用户
提问于 2014-02-13 12:25:24
回答 1查看 575关注 0票数 0

我为分配给我们的一个问题写了一些代码,而我当前的代码总是给我错误的输出。问题提示符如下:

你们中的一些人可能知道,没有比约翰更好的名字了。让我们定义比较名称的规则。每个字母都有一个权重('A‘- 1,'B’- 2,...,'Z‘- 26)。名称的权重是其所有字母权重的总和。例如,名称MARK的权重为13 +1+ 18 + 11 = 43。在比较两个名称时,权重较大的名称被认为是较好的。在平局的情况下,字典顺序上来得早的那个更好。但有一个例外--约翰这个名字是所有名字中最好的。您将获得一个String[]名称,其中的每个元素都包含一个名称。从最好到最差对名称进行排序,并返回排序后的String[]。

我写的代码如下:

代码语言:javascript
复制
public class TheBestName {
        public String[] sort(String[] names) {
            Arrays.sort(names, new APTComp());
            return names;
        }

        class APTComp implements Comparator<String>{
            public int compare(String a,String b){
                String alphabet= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                HashMap<Character,Integer> greatMap = new HashMap<Character,Integer>();
                for(int i=0;i<alphabet.length();i++){
                    greatMap.put(alphabet.charAt(i), i+1);
                }
                int countA=0;
                int countB=0;
                for(int i=0;i<a.length();i++){
                    int temp= greatMap.get(a.charAt(i));
                    countA+= temp;
                }
                for(int i=0;i<b.length();i++){
                    int temp=greatMap.get(b.charAt(i));
                    countB+=temp;
                }
                if(a.equals("JOHN") && b.equals("JOHN")){
                    return 0;
                }
                if(a.equals("JOHN") && !b.equals("JOHN")){
                    return 1;
                }
                if(!a.equals("JOHN") && b.equals("JOHN")){
                    return -1;
                }
                else{
                    int diff= countA-countB;
                    if(diff!=0){
                        return diff;
                    }
                    if(diff==0){
                        return a.compareTo(b);
                    }

                }
            }

        }



    }

在大多数情况下,我似乎得到了我应该得到的相反的结果。我试着摆弄compareTo方法,但没有什么不同。你们能告诉我我哪里做错了吗?

谢谢,朱奈德

EN

回答 1

Stack Overflow用户

发布于 2014-02-13 12:40:54

这就是我会怎么做,

代码语言:javascript
复制
     public int compare(String a, String b) {
        String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        HashMap<Character, Integer> greatMap = new HashMap<Character, Integer>();
        for (int i = 0; i < alphabet.length(); i++) {
            greatMap.put(alphabet.charAt(i), i + 1);
        }
        int countA = 0;
        int countB = 0;

        if (a.equals("JOHN")) {
            countA = Integer.MAX_VALUE;
        } else {
            for (int i = 0; i < a.length(); i++) {
                int temp = greatMap.get(a.charAt(i));
                countA += temp;
            }
        }
        if (b.equals("JOHN")) {
            countB = Integer.MAX_VALUE;
        } else {
            for (int i = 0; i < b.length(); i++) {
                int temp = greatMap.get(b.charAt(i));
                countB += temp;
            }
        }

        if (countB == countA) {
            return a.compareTo(b);
        }

        return countB - countA;

    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21745236

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档