Java:学生信息姓名、学号、成绩。定义一个类,并定义相关的构造函数和方法来设置和获取相关的学生信息?
老师布置的作业,求求大神解答一下!!! 展开
2020-07-31
1、代码如下:
Main类
import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
* Main 主方法
* @author bufei
*
*/
public class Main {
public static void main(String[] args) {
stuGrad();
}
public static void stuGrad() {
int stuNum = 1;
Student[] stu = new Student[stuNum];
double[] totals = new double[stuNum];
Map<Double, Integer> map = new HashMap<>(stuNum);
for (int i = 0; i < stuNum; i++) {
System.out.println("请输入第 " + (i + 1) + " 个学生信息:");
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("姓名:");
String name = reader.readLine();
System.out.println("学号:");
int id = Integer.parseInt(reader.readLine());
System.out.print("语文:");
double literature = Double.parseDouble(reader.readLine());
System.out.print("数学:");
double math = Double.parseDouble(reader.readLine());
System.out.print("英语:");
double english = Double.parseDouble(reader.readLine());
stu[i] = new Student(name, id, literature, math, english);
totals[i] = stu[i].getTotal();
map.put(stu[i].getTotal(), i);
} catch (Exception e) {
//TODO: handle exception
e.printStackTrace();
}
}
Arrays.sort(totals);
for (int j = stuNum - 1; j >= 0; j--) {
int i = map.get(totals[j]);
System.out.println(stu[i].toString());
}
}
}
2、Student类
public class Student {
private String name;
private int stuId;
//总分
private double total;
private double english;
//数学
private double mathematical;
//文学
private double literature;
public String getName(){
return name;
}
public void SetName(String name) {
this.name = name;
}
public int getStuId() {
return stuId;
}
public void setStuId(int stuId) {
this.stuId = stuId;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public Student() {
}
public double getTotal() {
return this.english + this.literature + this.mathematical;
}
public double getEnglish() {
return english;
}
public void setEnglish(double english) {
this.english = english;
}
public double getMathematical() {
return mathematical;
}
public void setMathematical(double mathematical) {
this.mathematical = mathematical;
}
public double getLiterature() {
return literature;
}
public void setLiterature(double literature) {
this.literature = literature;
}
public Student(int stuId, double total, double english, double mathematical, double literature) {
this.stuId = stuId;
this.total = total;
this.english = english;
this.mathematical = mathematical;
this.literature = literature;
}
public Student(String name, int stuId, double english, double mathematical, double literature) {
this.name=name;
this.stuId = stuId;
this.english = english;
this.mathematical = mathematical;
this.literature = literature;
}
@Override
public String toString() {
return "学生 " + name + ",学号 " + stuId + ",总分 =" + getTotal() + " [英语=" + english + ", 语文="
+ literature + ", 数学=" + mathematical + "]";
}
}
3、运行效果
但是我还要举例五个同学进行排序,而且还有一个科目是计算机,应该怎么做呢?麻烦答主帮忙看下,具体的详细题目在标题下方。
增加计算机科目,在Student类加一个computer属性,生成get set ,然后构造器和toString加上去就行了。
排序已经写了,你只需要修改main方法里面的stuNum = 5 ;就会输入5个学生信息。
排序用map存了一下成绩,如果分数相同,只会保留最后一个。