public class Student{
private String name;
private String studentNo;
private float score;
//get方法
//set方法
public static void main(String[] args){
//你可以用学生数组或者List<Student>来存数据
//我示范一下用数组存数据的 当然你用list来存的话 后面的比较 更加简单 只需要
//实现一个比较器即可
Student[] students = new Student[2]
Student a =new Student();
a.setName("a");
a.setStudentNo("001");
a.setScore(98);
students[0]=a;
Student b = new Student();
b.setName("b");
b.setStudentNo("002");
b.setScore(87);
sudents[1] = b;
。。。。。。
//然后用冒泡排序根据学生的成绩判断 算法你自己决定 冒泡还是挺好用的
for(int i =0,len =students.length ;i<len;i++){
for(int j=i+1,len;=students.length;j<len;j++){
if(students[i].getScore()>students[j].getScore()){
Student temp =students[i];
students[i] = students[j];
students[j]=temp;
}
}
}
///最后输出结果
for(){
System.out.println();
}
}
② Java中计算学生的平均身高
public class Compute {
class Student {// 学生类
private double height;
Student(double height) {
this.height = height;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
class Height {// 方法类
public double compute(Student[] students) {
int count = students.length;
double sum = 0;
for (Student student : students) {
sum += student.getHeight();
}
double result = sum / count;
return result;
}
}
class TestHeihgt {// 测试类
public void test() {
Student[] students = { new Student(170), new Student(160), new Student(165) };
Height height = new Height();
double avgHeight = height.compute(students);
System.out.println("学生平均身高是:" + avgHeight);
}
}
public static void main(String[] args) {
TestHeihgt testHeihgt = new Compute().new TestHeihgt();
testHeihgt.test();
}
}
纯手打,测试可用!