用java定义学生类(学号、姓名、成绩)。用列表list存放班级学生信息。然后按照成绩排序,并输出学生信息
4个回答
展开全部
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Admin {
public static void main(String... args) {
List data = new ArrayList();
Student s0 = new Student();
s0.setNo("000");
s0.setName("a");
s0.setChengji(100.0);
data.add(s0);
Student s1 = new Student();
s1.setNo("001");
s1.setName("b");
s1.setChengji(99.0);
data.add(s1);
Student s2 = new Student();
s2.setNo("002");
s2.setName("c");
s2.setChengji(98.0);
data.add(s2);
System.out.println("排序前");
show(data);
System.out.println("排序后");
sort(data);
show(data);
}
private static void sort(List data) {
Collections.sort(data, new Comparator() {
public int compare(Object o1, Object o2) {
Student s1 = (Student) o1;
Student s2 = (Student) o2;
return (int) (s1.getChengji() - s2.getChengji());
}
});
}
private static void show(List data) {
for (int i = 0; i < data.size(); i++) {
System.out.println(data.get(i));
}
}
}
class Student {
private String no;
private String name;
private double chengji;
public double getChengji() {
return chengji;
}
public void setChengji(double chengji) {
this.chengji = chengji;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String toString() {
return "学号:" + no + ",姓名:" + name + ",成绩:" + chengji;
}
}
结果
排序前
学号:000,姓名:a,成绩:100.0
学号:001,姓名:b,成绩:99.0
学号:002,姓名:c,成绩:98.0
排序后
学号:002,姓名:c,成绩:98.0
学号:001,姓名:b,成绩:99.0
学号:000,姓名:a,成绩:100.0
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Admin {
public static void main(String... args) {
List data = new ArrayList();
Student s0 = new Student();
s0.setNo("000");
s0.setName("a");
s0.setChengji(100.0);
data.add(s0);
Student s1 = new Student();
s1.setNo("001");
s1.setName("b");
s1.setChengji(99.0);
data.add(s1);
Student s2 = new Student();
s2.setNo("002");
s2.setName("c");
s2.setChengji(98.0);
data.add(s2);
System.out.println("排序前");
show(data);
System.out.println("排序后");
sort(data);
show(data);
}
private static void sort(List data) {
Collections.sort(data, new Comparator() {
public int compare(Object o1, Object o2) {
Student s1 = (Student) o1;
Student s2 = (Student) o2;
return (int) (s1.getChengji() - s2.getChengji());
}
});
}
private static void show(List data) {
for (int i = 0; i < data.size(); i++) {
System.out.println(data.get(i));
}
}
}
class Student {
private String no;
private String name;
private double chengji;
public double getChengji() {
return chengji;
}
public void setChengji(double chengji) {
this.chengji = chengji;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String toString() {
return "学号:" + no + ",姓名:" + name + ",成绩:" + chengji;
}
}
结果
排序前
学号:000,姓名:a,成绩:100.0
学号:001,姓名:b,成绩:99.0
学号:002,姓名:c,成绩:98.0
排序后
学号:002,姓名:c,成绩:98.0
学号:001,姓名:b,成绩:99.0
学号:000,姓名:a,成绩:100.0
更多追问追答
追问
那这个呢。。。麻烦了~! 定义一个学生类(姓名、学号、性别、分数)。构建一个向量对象,在里面存放20个学生信息。按照学号找出2009级学生的成绩平均值、2008级学生的平均值,2010级学生的成绩平均值。
追答
字数太多了,一次传不上去,你追问吧,我一次给你一点,你自己合一下
public static void main(String... args) {
List data = new ArrayList();
Student s0 = new Student();
s0.setNo("000");
s0.setLevel(2009);
s0.setName("a");
s0.setChengji(100.0);
data.add(s0);
Student s1 = new Student();
s1.setNo("001");
s1.setLevel(2009);
s1.setName("b");
s1.setChengji(99.0);
data.add(s1);
Student s2 = new Student();
s2.setNo("002");
s2.setLevel(2008);
s2.setName("c");
s2.setChengji(98.0);
data.add(s2);
Student s3 = new Student();
s3.setNo("003");
s3.setLevel(2008);
s3.setName("d");
s3.setChengji(97.0);
data.add(s3);
System.out.println("排序前");
show(data);
System.out.println("排序后");
sort(data);
show(data);
// 按照学号找出2009,2008,2007级学生的成绩平均值
// 从学号判断有点麻烦,加个属性level入学年份
data = group(data);
avg(data);
}
展开全部
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Test {
// 存放学生的list
private static List<Student> students = new ArrayList<Student>();
// 给students里添加数据
public static void init() {
students.add(new Student(10001, "张三", 99));
students.add(new Student(10002, "李四", 67));
students.add(new Student(10003, "王五", 88));
students.add(new Student(10004, "赵六", 44));
students.add(new Student(10005, "朱七", 51));
students.add(new Student(10006, "倩倩", 89));
}
// 对students进行排序
@SuppressWarnings("unchecked")
public static void sort() {
Collections.sort(students, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
int one = ((Student)o1).getScore();
int two = ((Student)o2).getScore();
return one - two;
}
});
}
public static void main(String[] args) {
init(); // 初始化
sort(); // 排序
// 输出显示
for(Student s : students) {
System.out.println("id:" + s.getId() + " name:" + s.getName() + " score:" + s.getScore());
}
}
}
// 学生类
class Student {
private int id;
private String name;
private int score;
public Student(int id, String name, int score) {
setId(id);
setName(name);
setScore(score);
}
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 int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Test {
// 存放学生的list
private static List<Student> students = new ArrayList<Student>();
// 给students里添加数据
public static void init() {
students.add(new Student(10001, "张三", 99));
students.add(new Student(10002, "李四", 67));
students.add(new Student(10003, "王五", 88));
students.add(new Student(10004, "赵六", 44));
students.add(new Student(10005, "朱七", 51));
students.add(new Student(10006, "倩倩", 89));
}
// 对students进行排序
@SuppressWarnings("unchecked")
public static void sort() {
Collections.sort(students, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
int one = ((Student)o1).getScore();
int two = ((Student)o2).getScore();
return one - two;
}
});
}
public static void main(String[] args) {
init(); // 初始化
sort(); // 排序
// 输出显示
for(Student s : students) {
System.out.println("id:" + s.getId() + " name:" + s.getName() + " score:" + s.getScore());
}
}
}
// 学生类
class Student {
private int id;
private String name;
private int score;
public Student(int id, String name, int score) {
setId(id);
setName(name);
setScore(score);
}
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 int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
对于学生类 可以直接声明
public class student(){
private int sno;
private String sname;
private int score;
}
然后set get方法
在action中放入list接入student类
前台列表直接获得list 然后迭代 这个是大体思路
有什么不懂可以追问。
public class student(){
private int sno;
private String sname;
private int score;
}
然后set get方法
在action中放入list接入student类
前台列表直接获得list 然后迭代 这个是大体思路
有什么不懂可以追问。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询