前言
定义学生分数类
//定义学生类
public class StudentScore {
private String name;
private double score;
public StudentScore() {
}
public StudentScore(String name, double score) {
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
主方法
package 计算分数系统;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;
public class TreesetStudentScore {
public static void main(String[] args) throws IOException {
Scanner reader = new Scanner(System.in);//扫描器
int number = 10;//老师默认为10 可更改
//-------------------------------------------------------------------------------
TreeSet<StudentScore> studentScores = new TreeSet<StudentScore>(new Comparator<StudentScore>() {//集合添加比较器
/*重写compare方法 与前一个数据比较 返回-1 添加到前一个数据后面 返回0 则比较器认为对象相同
不会添加 返回1 则添加到前一个数据前面
*/
public int compare(StudentScore s1, StudentScore s2) {
//主条件 分数高低
int num1 = (int) (s2.getScore() - s1.getScore());
//次条件 当分数相同 即num1==0
int num2 =/*后面为三目运算符*/num1 == 0 ? s1.getName().compareTo(s2.getName()) : num1;//当num1 ==0 判断名字-按字典顺序比较两个字符串
return num2;//num 的值可能为-1 0 1 各个数值的作用与上述相同
}
});
//--------------------------------------------------------------------------------------
while (true) {
//设置界面--------------------------------------------------------------------------------------
System.out.println("--------欢迎来到评分系统--------");
System.out.println("1.输入评委老师数量");
System.out.println("2.添加学生");
System.out.println("3.查看");
System.out.println("4.退出");
//读取x 以判断用户的需求
int x = reader.nextInt();
switch (x) {
case 1:
//结合实际 以防老师中途退场
System.out.println("请输入评委老师个数");
number = reader.nextInt();
System.out.println("success!");
break;
case 2:
//添加学生
addStudent(studentScores, number);
break;
case 3:
//遍历学生
ChectStudent(studentScores);
break;
case 4:
//java虚拟机退出
System.out.println("谢谢使用");
System.exit(0);
}
} //界面结束--------------------------------------------------------------------------------------
}
public static void addStudent(TreeSet<StudentScore> studentScores, int number) throws IOException {//添加学生的方法
//添加扫描器
Scanner sc = new Scanner(System.in);
//创建对象
StudentScore s = new StudentScore();
//从键盘读取名字
System.out.println("请输入学生姓名:");
String name = sc.nextLine();
//名字传递对象.name
s.setName(name);
//调用"从键盘读取分数"方法
System.out.println("请输入评委老师评分");
double sco[] = input(number);
//调用"获取平均分"方法
double y = ave(sco, number);
//对平均分数y进行格式化(四舍五入)
DecimalFormat m1 = new DecimalFormat("0.00");
m1.setRoundingMode(RoundingMode.HALF_UP);//HALF_UP — 四舍五入
s.setScore(Double.parseDouble(m1.format(y)));
/*将一个对象 即 name 和score 添加到TreeSet 集合
TreeSet 集合中借用比较器 实现分数的降序排列
*/
studentScores.add(s);
//-------------------------------------------------------------------------------
//将数据写入03.txt
BufferedWriter f1 = new BufferedWriter(new FileWriter(".//03.txt"));//字符缓冲流读数据 .代表当前目录
//增强for遍历 从TreeSet<StudentScore> studentScores中获取数据 且studentScores已排序
for (StudentScore s1 : studentScores) {
//StringBulider 来连接类的属性
StringBuilder sb = new StringBuilder();
sb.append("姓名:").append(s1.getName()).append(" ").append("分数:").append(s1.getScore());
//写入03.txt
f1.write(sb.toString());
//BufferWritre 特有方法 换行
f1.newLine();
//刷新
f1.flush();
}
//关闭IO流
f1.close();
System.out.println("success!");
}
//--------------------------------------------------------------------------------------------
public static void ChectStudent(TreeSet<StudentScore> studentScores) {//遍历
System.out.println("------------------------");
for (StudentScore s : studentScores) {
System.out.println("name: " + s.getName() + " score: " + s.getScore());
}
}
public static double ave(double a[], int number) {//获取平均分=(总分-最大值-最小值)/(老师数量-2)
double max = a[0], min = a[0], sum = 0;//初始化变量
for (int i = 0; i < a.length; i++) {
sum += a[i];//求和
if (a[i] > max) {
max = a[i];//获取最大值
}
if (a[i] < min) {
min = a[i];//获取最小值
}
}
return (sum - max - min) / (number - 2);//返回平均分
}
public static double[] input(int number) {//从键盘读取分数
double score[] = new double[number];
Scanner reader = new Scanner(System.in);
for (int i = 0; i < number; i++) {
score[i] = reader.nextDouble();
}
return score;
}
}
总结
以上就是本篇要讲的内容,希望自己可以一直不忘初心,砥砺前行,要想在茫茫人海中不被埋没,那就努力,拼搏吧,拼搏会给你成功的喜悦!
本段引用至 浩瀚星空 努力前行