普歌-允異團隊-學生成績:和,最大,平均值,成績大於60的人
阿新 • • 發佈:2021-02-04
技術標籤:java
學生成績
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共有,歡迎轉載,且在文章頁面明顯位置給出原文連結,未經作者同意必須保留此段宣告,否則保留追究法律責任的權利。