2012-02-29 83 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