确漏凯乱实挺复杂的,做了2个小时!
而且给的信息并不是很足,我自己添加了一个保存分数成绩的列表
希望楼主满意!
//MainTest .java
import java.util.Random;
public class MainTest {
public MainTest(){
String departname="计算机信息学院";
String className="软件工程1001";
//初始化班级
student_class aStuClass=new student_class(departname,className);
//10个学生的名字
String[] stuNames={"孙雹张山","李氏","网无","朝留","谈吧","赵平久","杨诗意","刘氏二","宋世山","向强"};
//添加10个学生
for(int i=0;i<stuNames.length;i++){
aStuClass.inputstudent(i, stuNames[i]);
}
//查找学号为5的学生
student idis5=aStuClass.searchstudent(5);
if(idis5==null){
System.out.println("没有学号为5的学生");
}else{
//把查到的这个学生信息输出来
System.out.println("学号为5的学生:");
idis5.showStudent();
}
//查找姓名为宋世山的学生
student nameSearch=aStuClass.searchstudent("宋世山"返档);
if(nameSearch==null){
System.out.println("没有叫宋世山的学生");
}else{
//把查到的这个学生信息输出来
System.out.println("\n名字叫宋世山的学生:");
nameSearch.showStudent();
}
//全班学生的名单
System.out.println("\n"+aStuClass.getTeam()+"的学生名单:");
aStuClass.output_student();
//成绩录入 语文
String yuwen="语文";
Random ram=new Random();
for(int i=0;i<5;i++){
aStuClass.input_score(i, yuwen, 40+ram.nextInt(60));
}
//英语成绩录入
String english="英语";
for(int i=0;i<10;i++){
aStuClass.input_score(i, english, 50+ram.nextInt(50));
}
//马克思成绩录入
String make="马克思";
for(int i=0;i<10;i++){
aStuClass.input_score(i, make, 60+ram.nextInt(40));
}
//显示几门课的成绩
String[] subject={"马克思","英语","语文"};
aStuClass.display_scores(subject);
}
public static void main(String args[]){
new MainTest();
}
}
//student_class.java
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class student_class {
private String depart;
private String team;
private Collection<student> member;
//新增加的分数成绩属性
private ArrayList<SubScore> score;
public student_class(){}
public student_class(String pDepart,String pTeam,Collection<student> pMember){
this.depart=pDepart;
this.member=pMember;
this.team=pTeam;
score=new ArrayList<SubScore>();
}
public student_class(String pDepart,String pTeam){
this.depart=pDepart;
this.team=pTeam;
this.member=new ArrayList<student>();
score=new ArrayList<SubScore>();
}
//输入学生
public void inputstudent(int i,String n){
student aStudent=new student(i,n);
member.add(aStudent);
}
//按照ID检索学生
public student searchstudent(int i){
student result=null;
Iterator<student> it = member.iterator(); // 获得一个迭代子
while(it.hasNext()) {
student aStu = (student)it.next(); // 得到下一个元素
if(aStu.getId() == i){
result=aStu;
break;
}
}
return result;
}
//按照名字检索学生
public student searchstudent(String name){
if(name==null){
return null;
}
student result=null;
Iterator<student> it = member.iterator(); // 获得一个迭代子
while(it.hasNext()) {
student aStu = (student)it.next(); // 得到下一个元素
if(aStu.getName().equals(name)){
result=aStu;
break;
}
}
return result;
}
//输出全班学生
public void output_student(){
Iterator<student> it = member.iterator(); // 获得一个迭代子
while(it.hasNext()) {
student aStu = (student)it.next(); // 得到下一个元素
System.out.println("学号:"+aStu.getId()+" 姓名:"+aStu.getName());
}
}
public void input_score(int id,String subject,int score){
SubScore aScore=new SubScore(id,subject,score);
this.score.add(aScore);
}
public void display_scores(String[] subject){
for(int i=0;i<subject.length;i++){
displayScore(subject[i]);
}
}
protected void displayScore(String subject){
//输出该课程的所有成绩
System.out.println("\n"+subject+"成绩一览:");
for(int j=0;j<score.size();j++){
SubScore aScore=score.get(j);
if(aScore.getSubject().equals(subject)){
aScore.showScore();
}
}
}
public String getDepart() {
return depart;
}
public void setDepart(String depart) {
this.depart = depart;
}
public String getTeam() {
return team;
}
public void setTeam(String team) {
this.team = team;
}
public Collection<student> getMember() {
return member;
}
public void setMember(Collection<student> member) {
this.member = member;
}
}
class student{
private int id;
private String name;
public student(){
}
public student(int id,String name){
this.id=id;
this.name=name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void showStudent(){
System.out.println("学号:"+id+" 姓名:"+name);
}
}
class SubScore{
private int id;
private String subject;
private int score;
public SubScore(){
}
public SubScore(int id,String subject,int score){
this.id=id;
this.subject=subject;
this.score=score;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public void showScore(){
System.out.println(" 学号:"+id+" 分数:"+score);
}
}
~~~~(>_<)~~~~ ,花了我两个小时啊!希望采纳哟