2013-10-14 83 views
0

Iam新的Java和我有一個Java Person類,我已經創建了人員列表,我可以打印列表,但我不想打印最小和最大年齡一個人名單 我的代碼是Java打印沒有最小值和最大值的列表

public class Person { 
    static String name; 
    static String lname; 
    static double age; 

    Person(String name,string lname,double age) { 
     Person.name = name; 
     Person.lname= lname; 
     Person.age = age; 
    } 
} 

    //Main method 
public void testQuestionInput() { 
    List<Person> persons = new ArrayList<Person>(); 
    persons.add(new Person("Foo", "scientist",5)); 
    persons.add(new Person("Foo", "scientist",4)); 
    persons.add(new Person("Foo", "teacher",10)); 
    persons.add(new Person("Bar", "student",11)); 
    persons.add(new Person("Foo", "scientist",12)); 

    for (Person person : _persons) { 
     System.out.print(person); 
    } 
} 

我能夠打印的清單,但我想打印在我所跳過我的情況下,最大和最小年齡我想打印列表而不

列表
"Foo", "scientist",4 and "Foo", "scientist",12 

感謝幫助將不勝感激。

+0

修復格式化! –

+0

你需要保持列表的順序嗎?另外,如果有最老的或最年輕的關係呢? –

+0

我想打印沒有最小和最大年齡的列表 – user1073794

回答

0

忽略綁定的可能性,可以通過遍歷列表兩次來完成此操作。第一次找到的最小和最大年齡和第二次做印刷:

public class Person { 
    String name; 
    String lname; 
    double age; 

    Person(String name, String lname, double age) { 
     this.name = name; 
     this.lname= lname; 
     this.age = age; 
    } 
} 

public void testQuestionInput() { 
    List<Person> persons = new ArrayList<Person>(); 
    persons.add(new Person("Foo", "scientist",5)); 
    persons.add(new Person("Foo", "scientist",4)); 
    persons.add(new Person("Foo", "teacher",10)); 
    persons.add(new Person("Bar", "student",11)); 
    persons.add(new Person("Foo", "scientist",12)); 
    double maxAge = Double.NEGATIVE_INFINITY; 
    double minAge = Double.POSITIVE_INFINITY; 
    for (Person person : _persons) { 
     minAge = Math.min(minAge, person.age); 
     maxAge = Math.max(maxAge, person.age); 
    } 
    for (Person person : _persons) { 
     if (person.age != minAge && person.age != maxAge) { 
      System.out.printf("%1$s, %2$s, %3$f%n", 
       person.name, person.lname, person.age); 
     } 
    } 
} 

你可能會需要定義PersontoString()方法從System.out.print(person)得到有意義的輸出就像你在你目前有碼。

+0

gibving infinty:/ – user1073794

+0

@ user1073794 - 糟糕。我初始化了'maxAge'和'minAge'。現在修復。 –

0

你的程序必須是這樣的:

import java.awt.List; 
import java.util.ArrayList; 

public class Person { 
    static String name; 
    static String lname; 
    static double age; 

    Person(String name, String lname, double age) { 
     Person.name = name; 
     Person.lname = lname; 
     Person.age = age; 

    } 

    // Main method 

    public void testQuestionInput() { 
     ArrayList<Person> persons = new ArrayList<Person>(); 
     persons.add(new Person("Foo", "scientist", 5)); 
     persons.add(new Person("Foo", "scientist", 4)); 
     persons.add(new Person("Foo", "teacher", 10)); 
     persons.add(new Person("Bar", "student", 11)); 
     persons.add(new Person("Foo", "scientist", 12)); 
     for (Person person : persons) { 

      System.out.print(person); 
     } 
    } 
} 
1

因爲它現在代表,你的程序將無法正常工作......

public class Person { 
    static String name; 
    static String lname; 
    static double age; 

    Person(String name,string lname,double age) { 
     Person.name = name; 
     Person.lname= lname; 
     Person.age = age; 
    } 
} 

採用static意味着,根據您的例如,您的所有參賽作品將命名爲Foo scientist並且年齡爲12

首先刪除static參考文獻...

public class Person { 

    String name; 
    String lname; 
    double age; 

    Person(String name,string lname,double age) { 
     this.name = name; 
     this.lname= lname; 
     this.age = age; 
    } 
} 

接下來,你可以對列表進行排序...

Collections.sort(persons, new Comparator<Person>() { 
    public int compare(Person p1, Person p2) { 
     return Double.compare(p1.age, p2.age); 
    } 
}); 

然後簡單地排除了第一個和最後條目...

for (int index = 1; index < persons.size() - 2; index++) { 
    Person p = persons.get(index); 
    System.out.println(p.name + " " + p.lname + " @ " + p.age); 
} 

例如...

接下來的問題是,如果你有多於一次的最小或最大年齡的條目會發生什麼?

你排序列表後,通過獲取下限和上限範圍開始...

int minAge = person.get(0).age; 
int maxAge = person.get(person.size() - 1).age; 

然後過濾掉那些不符合年齡boundries這些元素...

List<Person> withInLimites = new ArrayList<Person>(persons.size()); 
for (Person p : persons) { 
    if (p.age > minAge && p.age < maxAge) { 
     withInLimites.add(p); 
    } 
} 

for (Person p : withInLimites) { 
    System.out.println(p.name + " " + p.lname + " @ " + p.age); 
} 
+0

p1.age - p2.age會返回double,我想你需要在返回值之前將其轉換爲int – upog

+0

@upog停止指出明顯;) – MadProgrammer

+0

@upog希望至少可以解決這個問題:P – MadProgrammer