学生成绩
MikeCat
和,最大,平均值,成绩大于60的人
package LianXi;
/*
学生成绩:和,最大,平均值,成绩大于60的人
*/
import java.util.ArrayList;
public class Text02 {
public static void main(String[] args) {
ArrayList<Student> list = new ArrayList<>();
list.add(new Student("MikeCat",12));
list.add(new Student("Mike",23));
list.add(new Student("Cat",54));
list.add(new Student("麦克猫",89));
list.add(new Student("麦克猫Cat",67));
list.add(new Student("LL",78));
list.add(new Student("tr",52));
System.out.println("成绩最高:"+ getMax(list).getName() + " "+getMax(list).getScore());
System.out.println("学生成绩和:" + Sum(list));
System.out.println("学生平均值:" + Sum(list)/ list.size());
ArrayList<Student> list1 = getArrayList(list);
System.out.println("学生及格的人如下:" );
for (int i = 0; i < list1.size(); i++) {
Student s = list1.get(i);
System.out.println(s.getName()+ "::" + s.getScore());
}
}
public static Student getMax(ArrayList<Student> list){
Student max = list.get(0);
for (int i = 1; i < list.size(); i++) {
Student stu = list.get(i);
/*if (stu.getScore() > max.getScore()){
max = stu;
}*/
max = (stu.getScore() > max.getScore())?stu:max;
}
return max;
}
public static int Sum(ArrayList<Student> list){
int sum = 0;
for (int i = 0; i < list.size(); i++) {
sum += list.get(i).getScore();
}
return sum;
}
public static ArrayList<Student> getArrayList(ArrayList<Student> list){
ArrayList<Student> newList = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
Student stu = list.get(i);
if (stu.getScore() >= 60){
newList.add(stu);
}
}
return newList;
}
}
Student
package LianXi;
public class Student {
private String name;
private int score;
public Student() {
}
public Student(String name, int score) {
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
Run
成绩最高:麦克猫 89
学生成绩和:375
学生平均值:53
学生及格的人如下:
麦克猫::89
麦克猫Cat::67
LL::78
- 作者:麦克猫Cat
- 本文版权归作者和CSDN共有,欢迎转载,且在文章页面明显位置给出原文链接,未经作者同意必须保留此段声明,否则保留追究法律责任的权利。