2
回答
程序运行与预期效果不符,排序后生成的score.txt内没有第一科的成绩,
华为云实践训练营,热门技术免费实践!>>>   
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.text.DecimalFormat;
import java.util.Scanner;

//课程类
class Course {
	private int direct;  //课程学分
	private String name;  //课程名
	public Course(){}
	public Course(String name,int direct)
	{
		this.name = name;
		this.direct = direct;
	}
	public int getDirect() {
		return direct;
	}
	public String getName() {
		return name;
	}
	
	public void setDirect(int direct) {
		this.direct = direct;
	}
	public void setName(String name) {
		this.name = name;
	}
	
}
class Student {
	private String id;
	private String name;
	private Course[] myCous;	//每个学生的课程列表---由运行时动态输入
	private float[] score;
	public void setAveS(String aveS) {
	}
	public Student() {}
	public Student(String name, String id) {
		this.name = name;
		this.id = id;
	}
	public double getAveS() {
		float aves = 0f;
		for(int i=0; i<myCous.length; i++) {
			aves += score[i];
		}
		aves = (aves/score.length);
		return aves;
	}
	public String getId() {
		return id;
	}
	public Course[] getMyCous() {
		return myCous;
	}
	public String getName() {
		return name;
	}
	public void setId(String id) {
		this.id = id;
	}
	public void setMyCous(Course[] myCous) {
		this.myCous = myCous;
	}
	public void setName(String name) {
		this.name = name;
	}
	public float[] getScore() {
		return score;
	}
	public void setScore(int i) {
		this.score = new float[i];
	}
	public void setScore(float[] score) {
		this.score = score;
	}
}
public class test {
	static Course[] cous;
	final static int STUNUM = 10;
	static Student[] stus;
	public static void main(String[] args) throws Exception {
		setInfo();
		storeInfoToStuTxt();
		readInfoFromTxt();
		fun1();
		storeInfoToStuScore();
		fun2();
	}
	//输入相关信息
	public static void setInfo() {
		@SuppressWarnings("resource")
		Scanner in = new Scanner(System.in);
		stus = new Student[STUNUM];
		System.out.println("请输入一共有多少课程: ");
		int cousNum = Integer.parseInt(in.nextLine());		//课程总数
		cous = new Course[cousNum];
		System.out.println("请输入每门课程的名字及学分: ");
		for(int i=0; i<cousNum; i++) {
			String info = in.nextLine();
			String[] infos = info.split(" ");
			cous[i] = new Course(infos[0],Integer.parseInt(infos[1]));
		} 
		System.out.println("请输入10名学生的姓名、学号,各科目成绩,顺序为 "+showAllCourse2()+": ");
		
		//设置学生姓名  学号  课程成绩
		for(int i=0; i<STUNUM; i++) {
			String info = in.nextLine();
			String[] infos = info.split(" ");
			stus[i] = new Student(infos[0],infos[1]);
			Course[]  myC = stus[i].getMyCous();
			myC = new Course[cousNum];
			float[] myScore = stus[i].getScore();
			myScore = new float[cousNum];
			for(int j = 0;j < cousNum;j++)
			{
				myC[j] = new Course(cous[j].getName(),cous[j].getDirect());
				myScore[j] = Float.parseFloat(infos[j+2]);
			}
			stus[i].setMyCous(myC);
			stus[i].setScore(myScore);
		} 
	}
	//--------------------------------------------------->
	//找寻课程位置
	public static int matchCourse(String order) {
		int where = -1;		//获得命令课程的序号
		for(int i=0; i<cous.length; i++) {
			if(cous[i].getName().equals(order)) {
				where = i;
				break;
			} else {
				continue;
			}
		}
		if(where==-1) {
			System.out.println("没有该课程!");
			System.exit(0);			//退出。		---可设置为重新输入
		}
		return where;
	}
	//排序方法fun1()
	public static void fun1() {
		@SuppressWarnings("resource")
		Scanner in = new Scanner(System.in);
		System.out.println("请输入排序课程名及排序顺序: ");
		String order = in.nextLine();
		String[] or = order.split(" ");	//获得命令
		int where = -1;		//获得命令课程的序号
		for(int i=0; i<cous.length; i++) {
			if(cous[i].getName().equals(or[0])) {
				where = i;
				break;
			} else {
				continue;
			}
		}
		if(where==-1) {
			System.out.println("没有该课程!");
			System.exit(0);			//退出。		---可设置为重新输入
		}
		if(or[1].equals("降")) {
			for(int i=0; i<stus.length-1; i++) {
				for(int j=i+1; j<stus.length; j++) {
					if(stus[i].getScore()[where]<stus[j].getScore()[where]) {
						Student s = new Student();
						s = stus[i];
						stus[i] = stus[j];
						stus[j] = s;
					}	
				}
			}
		}
		else if(or[1].equals("升")) {
			for(int i=0; i<stus.length-1; i++) {
				for(int j=i+1; j<stus.length; j++) {
					if(stus[i].getScore()[where]>stus[j].getScore()[where]) {
						Student s = new Student();
						s = stus[i];
						stus[i] = stus[j];
						stus[j] = s;
					}	
				}
			}
		}
		System.out.println("排序完成!");
	}
	public static void storeInfoToStuScore() throws Exception {
		File file = new File("F:"+File.separator+"score.txt");
		FileOutputStream fos = new FileOutputStream(file);
		fos.write(("名次"+"\t"+"姓名"+"\t"+"学号"+showAllCourse()+"\t"+"平均分").getBytes());
		fos.write('\r');
		fos.write('\n');
		DecimalFormat df = new DecimalFormat("00.0");
		for(int i=0; i<stus.length; i++) {
			fos.write(("第"+(i+1)+"名"+"\t"+stus[i].getName()+"\t"+stus[i].getId()+"\t"+showAllScore(i)+(df.format(stus[i].getAveS()))).getBytes());
			fos.write('\r');
			fos.write('\n');
		}
		fos.close();
		System.out.println("写入成功,请查看!");
	}
	//读取数据  student.txt
	public static void readInfoFromTxt() throws Exception {
		stus = new Student[STUNUM];
		FileReader fr = new FileReader("F:"+File.separator+"student.txt");
		BufferedReader br = new BufferedReader(fr);
		String read = null;
		int cNum = 0;	//课程数
		String[] res = null;	//一行结果
		if((read=br.readLine())!=null) {
			res = read.split("\t");
			cNum = (res.length-2)/2;
		} else {
			System.out.println("文件无效!");	//获得课程数目
			System.exit(0);
		}
		cous = new Course[cNum];
		for(int i=0; i<cNum; i++) {
			Course c = new Course();
			cous[i] = c;
			cous[i].setName(res[2*i+2]);
		//			//设置课程名字
		}
		System.out.println("总共有下列课程:");
		for(int i=0; i<cNum; i++) {
			System.out.println(cous[i].getName());
		}
		int num=0;
		while((read=br.readLine())!=null) {
			res = read.split("\t");
			for(int j=0; j<cNum; j++) {
				Student s = new Student();
				stus[num] = s;
				stus[num].setName(res[0]);
				stus[num].setId(res[1]);
				stus[num].setScore(cNum);
				stus[num].getScore()[j] = Float.parseFloat(res[2*j+2]);				//------>此处读取正常  写出错误!
				cous[j].setDirect(Integer.parseInt(res[2*j+3]));
			}
			stus[num].setMyCous(cous);
			num++;
		}
		br.close();
		fr.close();
		System.out.println("读取完毕");
	}
	//二分法查询
	public static void fun2() {
		@SuppressWarnings("resource")
		Scanner in = new Scanner(System.in);
		System.out.println("请输入需要查询成绩的科目:");
		String cName = null;
		cName = in.nextLine();		//读入课程名
		int index = matchCourse(cName);		//课程名位置,若没有则退出!
		for(int i=0; i<stus.length-1; i++) {
			for(int j=i+1; j<stus.length; j++) {
				if(stus[i].getScore()[index]>stus[j].getScore()[index]) {
					Student s = new Student();
					s = stus[i];
					stus[i] = stus[j];
					stus[j] = s;
				}	
			}
		}
		float allScore[] = new float[stus.length];	//存储需要查询的某门课所有成绩
		for(int i=0; i<allScore.length; i++)
			allScore[i] = stus[i].getScore()[index];	
		System.out.println("请输入需要查询的分数: ");
		String sc = in.nextLine();
		float scores = Float.parseFloat(sc);
		int index2 = binarySearch(allScore, scores);	//	获得该成绩人员排名
		if(index2==-1)
			System.out.println("没有该成绩!");
		else {
			System.out.println(stus[index2].getName()+"\t"+stus[index2].getId()+"\t"+stus[index2].getMyCous()[index].getName()+"\t"+
					stus[index2].getScore()[index]);
		}
	}
	//二分查找  fun2核心
	public static int binarySearch(float[] srcArray,float des){
		int low=0;
		int high=srcArray.length-1;
		while(low<=high) {
		int middle=(low+high)/2;
		if(des==srcArray[middle]) {
			return middle;
		}else if(des<srcArray[middle]) {
			high=middle-1;
		} else {
			low=middle+1;
		}
		}
			return -1;
	}
	//存储相关信息
	public static void storeInfoToStuTxt() throws Exception {
		File file = new File("F:"+File.separator+"student.txt");
		FileOutputStream fos = new FileOutputStream(file);
		fos.write(("姓名"+"\t"+"学号"+showAllCourse()).getBytes());
		fos.write('\r');
		fos.write('\n');
		for(int i=0; i<stus.length; i++) {
			fos.write((stus[i].getName()+"\t"+stus[i].getId()+"\t"+showAllScore(i)).getBytes());
			fos.write('\r');
			fos.write('\n');
		}
		fos.close();
		System.out.println("写入完毕,请查看!");
	}
	public static String showAllScore(int num) {
		String str = "";
		for(int i=0; i<cous.length; i++) {
			str += (stus[num].getScore()[i]+"")+"\t"+(stus[num].getMyCous()[i]).getDirect()+"\t";
		}
		return str;
	}
	//显示所有课程名称排列(后缀"学分")
	public static String showAllCourse() {
		String courses = "";
		for(int i=0; i<cous.length; i++) {
			courses += "\t"+cous[i].getName()+"\t"+"学分";
		}
		return courses;
	}
	public static String showAllCourse2() {
		String courses = "";
		for(int i=0; i<cous.length; i++) {
			courses += cous[i].getName()+"\t";
		}
		return courses;
	}
	//显示科目及成绩列表
	public static String showAllCourseByStudent(int j) {
		String stuCourse = "";
		for(int i=0; i<cous.length; i++) {
			stuCourse += (stus[j].getMyCous())[i].getName()+"\t"+(stus[j].getScore())[i]+"\t";
		}
		return stuCourse;
	}
	//显示所有学生相关信息
	public static void showInfo() {
		String stuInfo = "";
		for(int i=0; i<STUNUM; i++) {
			stuInfo = stus[i].getName()+"\t"+stus[i].getId()+"\t"+showAllCourseByStudent(i);
			System.out.println(stuInfo);
		}
	}
}
<无标签>
举报
顶部