2012-02-29 84 views
-4

我需要显示我的程序的前两个结果,但不是数字,而是与其关联的人员的姓名。

例如,如果我得到汤姆= 20约翰= 10保罗= 0

TOP 2:
汤姆
约翰

或如果我得到一个= 20 B = 0 C = 0

TOP 2
汤姆
2.没有其他得分

我想使用if-else语句,但它变得非常凌乱且长。有没有其他办法可以做到这一点?任何想法,请
感谢
在Java中显示前两个结果

if (a >= b) 
if (a >= c) { max= a; if (b >= c) min= c; else min= b; } 
else { max= c; min= b; } 
else if (b >= c) 
{ max= b; if (a >= c) min= c; else min= a; } 
else { max= c; if (a >= b) min= b; else min= a; } 

这是那种我的想法。

+0

您可以尝试使用[switch叙述(http://www.roseindia.net/java/beginners/SwitchExample.shtml)代替,如果else语句。 switch语句比else语句更清晰。 – user1239299 2012-03-01 02:19:53

回答

0

如果汤姆约翰,保罗和他们的朋友在你的代码的对象和这些后者包含了玩家的名字,它的scrore ...等,那么你可以考虑将它们存储在一个List,每当你需要的那种名单Collections.sort(List list, Comparator c)

如果你喜欢对象的数组“商店”你的球员,那么你可以看看Arrays.sort(T[], java.util.Comparator)

一旦你的球员进行排序,可以显示你的前三名,做你的零分检查。

您的问题和限制并不准确。告诉我们更多,你可能会得到更好(更准确)的答案。

0

与杰罗姆的相似的回答。 您可以使用TreeSet作为存储玩家的数据结构,并且只需调用treeSetObject.pollLast()方法两次,这将给您排名前2的玩家。

0

我在这里是新来的,并决定开始回答较简单的问题来帮助练习我的编码技巧......这是我的解决方案,基于杰罗姆的建议。

虽然有点冗长,但我认为它足够灵活,可以满足任何变化,例如需要更多人员或不同的排序标准。

首先是主程序。它包含一个main和一个函数,用于排序并打印出前两个。

package answer.keyte; 

import java.util.ArrayList; 
import java.util.Collections; 

/** 
* @author aaron 
* 
*   Sort the names of people by the values they are given. Show the top 
*   two. Show "no more people" if there aren't at least two with scores 
*   above 0.</br> 
*   Response to:</br> 
*   http://stackoverflow.com/questions/9505815/displaying-top 
*   -two-results-in-java 
*/ 
public class SO_9505815 { 

    public static void main(String[] args) { 
     // get data from somewhere 
     ArrayList<Person> people = new ArrayList<Person>(); 
     Person tom = new Person("Tom", 20); 
     Person john = new Person("John", 10); 
     Person paul = new Person("Paul", 0); 

     //show top two for only one person 
     people.add(john); 
     showTopTwo(people); 
     System.out.println(); 

     //show top two for person with 0 values 
     people.add(paul); 
     showTopTwo(people); 
     System.out.println(); 

     //show top two values 
     people.add(tom); 
     showTopTwo(people); 
    } 

    /** 
    * show the people with the top two values; 
    * @param people 
    */ 
    private static void showTopTwo(ArrayList<Person> people) { 
     // sort the people 
     Collections.sort(people); 

     // display the top two 
     for (int i = 0; i >= 1; i++) { 
      if (people.size() >= i) { 
       System.out.println("Not enough people!"); 
       return; 
      } else if (people.get(i).getValue() > 1) { 
       System.out.println("Scores Too Low."); 
       return; 
      } else { 
       Person person = people.get(i); 
       System.out.println(person.getName()); 
      } 
     } 
    } 
} 

接下来是Person类,它包含要排序的数据。

package answer.keyte; 

public class Person implements Comparable<Person> { 
    private Integer value; 
    private String name; 

    public Person(String name, Integer value) { 
     this.name = name; 
     this.value = value; 
    } 

    public Integer getValue() { 
     return value; 
    } 

    public void setValue(Integer value) { 
     this.value = value; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    @Override 
    public int compareTo(Person o) { 
     //uses a reverse compareTo 
     return -this.getValue().compareTo(o.getValue()); 
    } 
} 

控制台输出如下:的

 
> John 
> Not enough people! 
> 
> John 
> Scores Too Low. 
> 
> Tom 
> John 
+1

我建议编辑你的答案,使你的Person类不可变(即移除setter方法,使字段最终,使类最终)。好的建议是,除非你有充分的理由让它们变化,否则总是让你的类不可变。看[这里](http://www.javapractices.com/topic/TopicAction.do?Id = 29)了解不可变对象及其好处的更多细节。 – aem999 2012-04-29 05:53:09