gpt4 book ai didi

java - 如何找到某些参数?

转载 作者:行者123 更新时间:2023-12-01 13:19:10 25 4
gpt4 key购买 nike

例如我有:

ArrayList<Student> stu = new ArrayList<Student>();
stu.add(new HighSchoolStudent("Jem", "Finch", 11, 3.4));
stu.add(new Student("Scout", "Finch", 4));
stu.add(new HighSchoolStudent("Boo", "Radley", 12, 1.7));
stu.add(new HighSchoolStudent("Atticus", "Finch", 12, 4.8));
stu.add(new Student("Patrick", "Henry", 9));
stu.add(new Student("Patrick", "Henry", 11));

ArrayList<Teacher> tea = new ArrayList<Teacher>();
tea.add(new Teacher("Betsy", "Ross", "Home Ec"));
tea.add(new Teacher("Ada", "Lovelace", "Mathematics"));
tea.add(new Teacher("Grace", "Hopper", "Computer Science"));
tea.add(new Teacher("Marie", "Curie", "Chemistry"));
tea.add(new Teacher("Dolly", "Madison", "Government"));
tea.add(new Teacher("Maya", "Angelou", "English Composition"));

School sch = new School(stu, tea);
System.out.println("\n\njust seniors: \n" + sch.getGradeLevel(12));

并且 HighSchoolStudent 和 Student 都有参数 (LastName,FirstName,GradeLevel,gpa)。我如何在另一个类中创建一个方法(getGradeLevel),当 GradeLevel 为 12 时,它只打印 arrayList 的那部分?所以我希望输出是:

Radley, Boo
Grade Level: 12
ID #: 5
GPA: 1.7
Finch, Atticus
Grade Level: 12
ID #: 6
GPA: 4.8

我的学校类(class)代码是

class School
{
public ArrayList <Student> student = new ArrayList<Student>();
public ArrayList <Teacher> teacher = new ArrayList<Teacher>();
public String listOfNames;
private String gradeList;
public School()
{
}
public School(ArrayList <Student> stu, ArrayList <Teacher> tec)
{
student = stu;
teacher = tec;
}

public String getGradeLevel(int grade)
{
//Need help with this part
}




public String toString()
{//Makes a string with last, first name and subject
listOfNames+=("\nFaculty:\n");
for (int i=0;i<teacher.size();i++)
{
listOfNames +=" "+teacher.get(i);
}

listOfNames+="\nStudents:\n";
for (int i=0;i<student.size();i++)
{
listOfNames +=student.get(i)+" ";
}
return listOfNames;
}//End toString
}

最佳答案

for(Student s:stu){
if(s.getGradeLevel()==12){
System.out.println(s);
}
}

此答案假设您已在 Student 类中声明了一个返回学生年级的方法 getGradeLevel(),并且您已正确重写 Student .toString().

关于java - 如何找到某些参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22187555/

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