package zuoYe;
import java.util.ArrayList;
import java.util.Scanner;
import com.heima.bean.Student;
import com.heima.bean.Student01;
public class zuoye04 {
/**
* 分析以下需求,并用代码实现:
(1)定义一个学生类Student,属性:姓名(String name)、班级班号(String no)、成绩(double score)
(2)将若干Student对象存入List集合,并统计每个班级的总分和平均分
思路:
a.采用面向对象的思想
b.不推荐使用Map<String,List<Student>> 操作不方便
c.推荐使用Map<String,Student>
*/
public static void main(String[] args) {
ArrayList<Student01> a1 = new ArrayList<>();
ArrayList<Student01> a2 = new ArrayList<>();
Scanner sc = new Scanner(System.in);
int count1 = 0;
int count2 = 0;
for(int i=0;i<3;i++){
System.out.println("请输入一个学生信息按照姓名,成绩,班级");
String s = sc.nextLine();
String[] sp = s.split(",");
int in =Integer.parseInt(sp[sp.length-1]);
double d = Double.parseDouble(sp[sp.length-2]);
String str = sp[sp.length-3];
if(in==1){
a1.add(new Student01(str,d,in));
count1++;
}
if(in==2){
a2.add(new Student01(str,d,in));
count2++;
}
}
Double sum1 = 0.0;
Double sum2 = 0.0;
for (Student01 stu : a1) {
sum1 = sum1 +stu.getScore();
System.out.println(stu.getNo()+"班"+stu.getName()+"分数是"+stu.getScore());
}
for (Student01 stu : a2) {
sum2 = sum2 +stu.getScore();
System.out.println(stu.getNo()+"班"+stu.getName()+"分数是"+stu.getScore());
}
double average1 = sum1/count1;
double average2 = sum2/count2;
System.out.println("1班总成绩是"+sum1+"平均分是"+average1+"二班总成绩是"+sum2+"平均分是"+average2);
}
}
|
|