日期:2014-05-20  浏览次数:20685 次

如何实现A.deleteStudent(ww)并输出???
/*组合的使用
1、创建一个Student类,有name、computerScore、mathScore三个成员变量及相应的getXXX方法。
2、创建一个BanJi类,它有一个学生数组作为成员变量,并有添加学生和删除学生、求班级所有学生总成绩和平均成绩的方法.
3、创建一个Test类,它有一个main函数,在该类中创建一些学生并将这些学生添加到班级类中,求出整个班级所有学生的平均成绩和总成绩。
 */
//1、创建一个Student类,有name、computerScore、mathScore三个成员变量及相应的getXXX方法。
class Student {
String name;
float computerScore, mathScore;

Student(String name, float computerScore, float mathScore) {
this.name = name;
if (computerScore >= 0 && computerScore <= 100)
this.computerScore = computerScore;
if (mathScore >= 0 && mathScore <= 100)
this.mathScore = mathScore;
}

String getName() {
return name;
}

float getComputerScore() {
return computerScore;
}

float getMathScore() {
return mathScore;
}
}

// 2、创建一个BanJi类,它有一个学生数组作为成员变量,并有添加学生和删除学生、求班级所有学生总成绩和平均成绩的方法.
class BanJi {
Student stus[];
int count = 0;

BanJi(int num) {
stus = new Student[num];
}

void addStudent(Student s) {
stus[count++] = s;
}

void deleteStudent(Student s) {
for (int i = 0; i < stus.length; i++) {
if (stus[i].name.equals(s.name)) {
stus[i] = null;
}
}
}

float getComTatol() {
float sum = 0;
for (int i = 0; i < stus.length; i++) {
sum = sum + stus[i].getComputerScore();
}
return sum;
}

float getMathTatol() {
float sum = 0;
for (int i = 0; i < stus.length; i++) {
sum = sum + stus[i].getMathScore();
}
return sum;
}

float getComAvg() {
return getComTatol() / stus.length;
}

float getMathAvg() {
return getMathTatol() / stus.length;
}
}

// 3、创建一个Test类,它有一个main函数,在该类中创建一些学生并将这些学生添加到班级类中,求出整个班级所有学生的平均成绩和总成绩。
public class Test {
public static void main(String args[]) {
Student zs = new Student("张三", 89, 95);
Student ls = new Student("李四", 85, 90);
Student ww = new Student("王五", 80, 80);
BanJi A = new BanJi(3);
A.addStudent(zs);
A.addStudent(ls);
A.addStudent(ww);
System.out.println("计算机科目的总成绩:" + A.getComTatol());
System.out.println("数学科目的总成绩:" + A.getMathTatol());
System.out.println("计算机科目的平均成绩" + A.getComAvg());
System.out.println("数学科目的平均成绩" + A.getMathAvg());
A.deleteStudent(ww);
System.out.println("计算机科目的总成绩:" + A.getComTatol());
System.out.println("数学科目的总成绩:" + A.getMathTatol());
System.out.println("计算机科目的平均成绩" + A.getComAvg());
System.out.println("数学科目的平均成绩" + A.getMathAvg());
}
}

------解决方案--------------------
不懂你的A.deleteStudent(ww)并输出,我理解的就是

Student deleteStudent(Student s) {
 Student student = new Student();
for (int i = 0; i < stus.length; i++) {
if (stus[i].name.equals(s.name)) {
student = stus[i];
stus[i] = null;
}
}
return student;
}
当然,你要在student类中增加一个空的构造方法
------解决方案--------------------
[code=Java][/code]import java.util.ArrayList;
import java.util.List;

class Student {
String name;
float computerScore, mathScore;

Student(String name, float computerScore, float mathScore) {
this.name = name;
if (computerScore >= 0 && computerScore <= 100)
this.computerScore = computerScore;
if (mathScore >= 0 && mathScore <= 100)
this.mathScore = mathScore;
}

String getName() {
return name;
}

float getComputerScore() {
return computerScore;