gpt4 book ai didi

Java编程: trying to sort by age

转载 作者:行者123 更新时间:2023-11-30 07:58:53 26 4
gpt4 key购买 nike

我正在编写一个小程序,显示三位教授并打印其中一位最年长和最年轻的教授。我认为我的 if 语句是正确的,但它为两个语句打印出相同的名称。

public class app {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String initName = "Megan";
String lastName = "Smith";
String Degree = "Economics";
int age = 40;
professor2 pr1 = new professor2 (initName,lastName, Degree, age);

professor2 pr2 = new professor2 ("John","White","Statistics", 55);

professor2 pr3 = new professor2 ("Jill","Black","History", 60);


//Check Oldest
if ((pr1.getAge() > pr2.getAge()) && (pr1.getAge() > pr3.getAge())){
System.out.println(pr1.getName() + " is older");}

if ((pr2.getAge() > pr1.getAge()) && (pr2.getAge() > pr3.getAge())){
System.out.println(pr1.getName() + " is older");}

if ((pr3.getAge() > pr1.getAge()) && (pr3.getAge() > pr1.getAge())){
System.out.println(pr1.getName() + " is older");}

//Chech Youngest
if ((pr1.getAge() < pr2.getAge()) && (pr1.getAge() < pr3.getAge())){
System.out.println(pr1.getName() + " is younger");}

if ((pr2.getAge() < pr1.getAge()) && (pr2.getAge() < pr3.getAge())){
System.out.println(pr1.getName() + " is younger");}

if ((pr3.getAge() < pr1.getAge()) && (pr3.getAge() < pr1.getAge())){
System.out.println(pr1.getName() + " is younger");}

}



}

}

那么我的第二个文件是:

public class professor2 {
String firstName;
String lastName;
String Degree;
int age;

professor2 (String myFirstName, String myLastName,String myDegree, int myAge)
{
firstName = myFirstName;
lastName= myLastName;
Degree = myDegree;
age = myAge;
}

String getName()
{
return firstName + " " +lastName;
}

String getDegree()
{
return Degree;
}

int getAge()
{
return age;
}
}

最佳答案

您似乎在这里复制粘贴了相同的语句。无论 if 中的条件如何,您都会打印 pr1 的详细信息。修复这些语句应该会给你正确的输出:

//Check Oldest
if ((pr1.getAge() > pr2.getAge()) && (pr1.getAge() > pr3.getAge())) {
System.out.println(pr1.getName() + " is older");
}

if ((pr2.getAge() > pr1.getAge()) && (pr2.getAge() > pr3.getAge())) {
System.out.println(pr2.getName() + " is older"); // here
}

if ((pr3.getAge() > pr1.getAge()) && (pr3.getAge() > pr1.getAge())) {
System.out.println(pr3.getName() + " is older"); // and here
}

//Check Youngest
if ((pr1.getAge() < pr2.getAge()) && (pr1.getAge() < pr3.getAge())) {
System.out.println(pr1.getName() + " is younger");
}

if ((pr2.getAge() < pr1.getAge()) && (pr2.getAge() < pr3.getAge())) {
System.out.println(pr2.getName() + " is younger"); // and here
}

if ((pr3.getAge() < pr1.getAge()) && (pr3.getAge() < pr1.getAge())) {
System.out.println(pr3.getName() + " is younger"); // and here
}

这种方法有效,但无法扩展。如果你有十个教授怎么办?还是一百?还是一千?更强大的解决方案可能是将这些对象存储在集合中并使用自定义比较器对其进行排序:

List<professor2> list = new ArrayList<>(Arrays.asList(pr1, pr2, pr3);
Collections.sort(list, new Comparator<professor2>() {
@Override
public int compare (professor2 p1, professor2 p2) {
return Integer.compare(p1.getAge(), p2.getAge());
}
}
);
System.out.println ("The oldest is " + list.get(list.size() - 1).getName());
System.out.println ("The youngest is " + list.get(0).getName());

关于Java编程: trying to sort by age,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32237497/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com