如何使用s1.computeGrade(69)...显示我的arrayList,并将其打印出来

时间:2015-11-15 07:22:12

标签: java arrays

这是我的班级

package studentresults;

public class Student 
{
private String name, subject, grade;

public Student(String name, String subject)
{
    this.name = name;
    this.subject = subject;
}

public String getGrade()
{
    return grade;
}

public void computeGrade(double marks)
{
    if(marks >= 80)
    {
        this.grade = "A";
    }
    else if (marks>=70)
    {
        this.grade = "B";
    }
    else if (marks>=60)
    {
        this.grade = "C";
    }
    else if (marks>=50)
    {
        this.grade = "D";
    }
    else
    {    
        this.grade = "F";   
    }
}
 public void computeGrade(String score)
{
    if (score == "Pass")
    {
        this.grade = "P";
    }
    else
    {
        this.grade = "F";
    }
}

public String toString()
 {
    String info = "Name: " + name + "\t Subject: " + subject + "\t Grade: "    + grade;
    return info;
}
}

接下来是我的主要内容:

 package studentresults;

 import java.util.ArrayList;

 public class StudentResults 
 {
public static void main(String[] args) 
{
    ArrayList<Student> stuList = new ArrayList<>();
    ArrayList newList = new ArrayList();

    Student s1 = new Student("Clara Ng", "Basic Accounting");
    Student s2 = new Student("Kate Sun", "Basic Accounting");
    Student s3 = new Student("Ray Wong", "French Language");
    Student s4 = new Student("Nadirah", "Creative Writing");
    Student s5 = new Student("Fabian Ho", "Basic Accounting");
    Student s6 = new Student("Tan Kexin", "Creative Writing");

    stuList.add(s1);
    stuList.add(s2);
    stuList.add(s3);
    stuList.add(s4);
    stuList.add(s5);
    stuList.add(s6);

   s1.computeGrade(79);
   s2.computeGrade("Pass");
   s3.computeGrade(65.5);
   s4.computeGrade(85);
   s5.computeGrade("Fail");
   s6.computeGrade(74.5);

   newList.addAll(stuList);

   for(int i=0;stuList.length;i++)
   {
       if()
      {
           //System.out.println(i.toString());
      }

   }

}

}

1)使用在Student类中创建的arrayList / vector和方法,将所有未评分的通过学生的详细信息打印到屏幕上。

2)使用在Student类中创建的arrayList / vector和方法,将等级为B或更高的学生总数打印到屏幕上。

2 个答案:

答案 0 :(得分:0)

只需迭代它,例如第二个问题:

for(student : stuList) {
    if("B".equals(student.getGrade()) || "A".equals(student.getGrade())) {
        student.toString();
    } 
} 

你可以为第一个更容易做同样的事情! 希望这可以帮助。

答案 1 :(得分:0)

修改Student班级使用.equals()来比较字符串

 public void computeGrade(String score)
{
    if (score.equals("Pass"))
    {
        this.grade = "P";
    }
    else
    {
        this.grade = "F";
    }
}

你的主要:

 int highGradeStudents=0;
    for(int i=0;i < stuList.size();i++)
     {
         if(!stuList.get(i).getGrade().equals("P"))//all the non-graded pass students
         {
               System.out.println(stuList.get(i).toString());
         }
         if(stuList.get(i).getGrade().equals("B") || stuList.get(i).getGrade().equals("A")) {
         //calculate number of students whose grade was B or better
               highGradeStudents++;
         }
       }
    System.out.println("the total number of students whose grade is B or better  = "+highGradeStudents);