[Java] 纯文本查看 复制代码
package com.it.day07.Demo09;
import java.util.ArrayList;
import java.util.concurrent.SynchronousQueue;
public class Demo05StudentMain {
public static void main(String[] args) {
/*
* 定义学员数组,并把学员信息add
*/
ArrayList<Demo05Student> arrayList = new ArrayList<>();
Demo05Student ds1 = new Demo05Student("马云",30,80);
Demo05Student ds2 = new Demo05Student("马化腾",20,78);
Demo05Student ds3 = new Demo05Student("刘强东",19,77);
Demo05Student ds4 = new Demo05Student("李彦宏",29,88);
Demo05Student ds5 = new Demo05Student("刘信飞",24,99);
Demo05Student ds6 = new Demo05Student("王建立",40,34);
Demo05Student ds7 = new Demo05Student("王思聪",30,60);
Demo05Student ds8 = new Demo05Student("王健林",50,66);
Demo05Student ds9 = new Demo05Student("马凯",25,50);
Demo05Student ds10 = new Demo05Student("无名",22,100);
arrayList.add(ds1);
arrayList.add(ds2);
arrayList.add(ds3);
arrayList.add(ds4);
arrayList.add(ds5);
arrayList.add(ds6);
arrayList.add(ds7);
arrayList.add(ds8);
arrayList.add(ds9);
arrayList.add(ds10);
//获取成绩最高的学员
System.out.println(maxScore(arrayList));
StringBuilder sb = new StringBuilder();
System.out.println(sb.append("获取成绩最高的学员 姓名:").append(maxScore(arrayList).getName())
.append("年龄:").append(maxScore(arrayList).getAge()).append("成绩:").append(maxScore(arrayList).getScore()));
//获取全部学员的总成绩
System.out.println("获取学员的总成绩:" + zScore(arrayList) + "分");
//获取全部学员的平均分
System.out.println("获取学员的平均分:" + average(arrayList) + "分");
}
/**
* 获取成绩最高的学生对象
* @param arrays
* @return
*/
public static Demo05Student maxScore(ArrayList<Demo05Student> arrays){
//定义一个返回对象
Demo05Student ds = new Demo05Student();
double maxScore = 0.0;
//定义一个索引记录最大成绩的索引
int maxIndex = 0;
for (int i = 0; i < arrays.size(); i++) {
if(maxScore < arrays.get(i).getScore()){
maxScore = arrays.get(i).getScore();
maxIndex++;
}
}
//把最高的索引成绩的学员set对象里
ds.setName((arrays.get(maxIndex).getName()));
ds.setAge((arrays.get(maxIndex).getAge()));
ds.setScore((arrays.get(maxIndex).getScore()));
return ds;
}
/**
* 总成绩
* @param arrays
* @return
*/
public static double zScore(ArrayList<Demo05Student> arrays){
double zscore = 0;
for (int i = 0; i < arrays.size(); i++) {
zscore += arrays.get(i).getScore();
}
return zscore;
}
/**
* 获取学员的平均分
* @param arrays
* @return
*/
public static double average(ArrayList<Demo05Student> arrays){
double ag = 0;
for (int i = 0; i < arrays.size(); i++) {
ag = ag += arrays.get(i).getScore()/arrays.size();
}
return ag;
}
}