怎么通过做网站来赚钱,企业管理定制软件,临沂做商城网站建设,wordpress英文如何改中文题目要求#xff1a; 键盘录入5个学生信息#xff08;姓名#xff0c;语文成绩#xff0c;数学成绩#xff0c;英语成绩#xff09;#xff0c;按照总分从高到低输出到控制台
分析#xff1a; 1#xff0c;定义一个学生类 * 成员变量#xff1a;姓名#xff0c;…题目要求 键盘录入5个学生信息姓名语文成绩数学成绩英语成绩按照总分从高到低输出到控制台
分析 1定义一个学生类 * 成员变量姓名语文成绩数学成绩英语成绩 * 成员方法空参构造有参构造有参构造的参数分别是姓名语文成绩数学成绩英语成绩 * toString方法遍历集合中的Student对象打印对象引用的时候会显示属性值 * 2键盘录入需要Scanner创建键盘录入对象 * 3创建TreeSet集合对象在TreeSet的构造函数中 传入比较器按照总分比较 * 4录入五个学生所以以集合中的学生个数为判断条件如果size是小于5就进行存储 * 5将录入的字符串切割用逗号切割会返回一个字符串数组将字符串数组中从第二个元素转换成int数组 * 6将转换后的结果封装成Student对象将Student添加到TreeSet集合中 * 7遍历TreeSet集合打印每一个Student对象
package com.wsq.test;import java.util.Comparator;import java.util.Scanner;import java.util.TreeSet;import com.wsq.bean.Student;public class Test7 {//2键盘录入需要Scanner创建键盘录入对象Scanner wsq new Scanner(System.in); System.out.println(请输入学生成绩格式是姓名语文成绩数学成绩英语成绩);//3创建TreeSet集合对象在TreeSet的构造函数中 传入比较器按照总分比较TreeSetStudent yy new TreeSet(new ComparatorStudent() {Overridepublic int compare(Student s1, Student s2) {int num s2.getSum() - s1.getSum();return num 0 ? 1 :num ;}});//4录入五个学生所以以集合中的学生个数为判断条件如果size是小于5就进行存储while(yy.size() 5){//5将录入的字符串切割用逗号切割会返回一个字符串数组将字符串数组中从第二个元素转换成int数组String line wsq.nextLine();String [] arr line.split(,);int chinese Integer.parseInt(arr[1]);int math Integer.parseInt(arr[2]);int english Integer.parseInt(arr[3]);//6将转换后的结果封装成Student对象将Student添加到TreeSet集合中yy.add(new Student(arr[0],chinese,math,english));}//7遍历TreeSet集合打印每一个Student对象System.out.println(排序后的学生信息);for (Student s : yy) {System.out.println(s);}}}
package com.wsq.bean;
/*1定义一个学生类成员变量姓名语文成绩数学成绩英语成绩成员方法空参构造有参构造有参构造的参数分别是姓名语文成绩数学成绩英语成绩toString方法遍历集合中的Student对象打印对象引用的时候会显示属性值
*/
public class Student {private String name;private int chinese;private int math;private int english;private int sum;public Student() {super();}public Student(String name, int chinese, int math, int english) {super();this.name name;this.chinese chinese;this.math math;this.english english;this.sum this.chinese this.math this.english;}public int getSum() {return sum;}public String toString(){return name , chinese , math , english , sum;}}