一、实验目的
- 学习应用泛型编写程序
- 掌握使用枚举类型编写程序,学会运用for语句简化。
实验内容:
必做
1、 for语句的简化,对象数组,编写程序,满足以下条件:(需讲解)
- 新建一个学生Student类,属性包括学号、姓名、数学、英语和计算机三门课程成绩及平均分。
- 新建一个学生班级类StudentClass,包含变量一个学生数组长度为5,代表5名学生Student;
- 在构造方法中新建5位学生的基本信息,要求可以通过键盘输入中修改学生除学号、平均分之外的信息;
- 要求该类具有计算学生平均分并最终输出每位学生的学号、姓名、平均分。
- (尽量用for语句简化形式来做)
(键盘输入请参考实验二中的testInput.java文件)
实验解答
- 创建
Student
类
package ex08;
public class Student {
String num;//学号
String name;//姓名
float Mathematical;//数学
float English;//英语
float compurter;//计算机
float AverageScore = (Mathematical + English + compurter)/3;
//float AverageScore = (Mathematical + English + compurter)/3;//平均分
//构造方法
public Student(String num,String name,float Mathematical,float English,float compurter){
this.num = num;
this.name = name;
this.Mathematical = Mathematical;
this.English = English ;
this.compurter = compurter ;
AverageScore = (Mathematical + English + compurter)/3;
//System.out.println("学号:"+num+" "+"姓名:"+name+" "+"平均分:"+AverageScore );
}
/*
public static void m1() {
System.out.println(num+" "+name+" "+Mathematical+" " +English+" " +compurter+" " +AverageScore );
}
*/
}
- 创建
StudentClass
类
package ex08;
import java.util.Scanner;
public class StudentClass {
public static void main(String[] args) {
Student[] mate = new Student[5];
//构造方法赋值
Student zhangsan = new Student("1801","zhangsan",59,58,57);
Student lisi = new Student("1802","lisi",59,58,57);
Student wangwu = new Student("1803","wangwu",59,58,57);
Student maliu = new Student("1804","maliu",59,58,57);
Student zhaoqi = new Student("1805","zhaoqi",59,58,57);
//zhangsan.AverageScore;
//给学生数组赋值
mate[0] = zhangsan;
mate[1] = lisi;
mate[2] = wangwu;
mate[3] = maliu;
mate[4] = zhaoqi;
for (Student i :mate) {
System.out.println(i.num+" "+i.name+" "+i.Mathematical+" "+i.English+" "+i.compurter+" "+i.AverageScore );
System.out.println("是否修改,是请输入1,否则输入0");
Scanner s = new Scanner(System.in);
boolean c = s.hasNext();
if(c) {
System.out.println("姓名:");
Scanner s1 = new Scanner(System.in);
i.name = s1.next();
System.out.println("数学成绩:");
Scanner s2 = new Scanner(System.in);
i.Mathematical = s2.nextInt();
System.out.println("英语成绩:");
Scanner s3 = new Scanner(System.in);
i.English = s3.nextInt();
System.out.println("计算机成绩:");
Scanner s4 = new Scanner(System.in);
i.compurter = s4.nextInt();
System.out.println("修改后的信息");
System.out.println("姓名:" + i.name + "学号:" + i.num + "计算机成绩:" + "平均分:" );
System.out.println("------------------");
}else {
System.out.println("姓名:" + i.name + "学号:" + i.num + "平均分:" );
System.out.println("-------------");
}
}
}
}
实验总结:
学习了数组及对象数组的使用