我的Java课程中有一个最终作业。我不理解,老师吹。请帮忙!! 这是作业:
我无法通过GPA进行排序,就像作业所说它需要采用GPA序列一样。
这是第一个文件:
5
Aaron
Smith
4
Dallas
Lambert
3
Mike
Scott
2
George
Mcfly
1
Greg
Peterson
这是第二个文件:
1
4
3.2
2
5
2.2
3
6
3.0
4
3
4.0
5
2
3.5
请注意,两个文件中的第一个数据元素是学生编号。
这是我的程序代码:
public static void main(String[] args) throws FileNotFoundException, IOException {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first file name: ");
String firstFile = "/Volumes/Sandisk/Personal/" + scanner.next();
File file = new File(firstFile);
FileReader fileReader = new FileReader(firstFile);
BufferedReader buffReader = new BufferedReader(fileReader);
//Second File input
Scanner scannerTwo = new Scanner(System.in);
System.out.print("Enter second file name: ");
String secondFile = "/Volumes/Sandisk/Personal/" + scannerTwo.next();
File fileTwo = new File(secondFile);
FileReader fileReaderTwo = new FileReader(secondFile);
BufferedReader buffReaderTwo = new BufferedReader(fileReaderTwo);
int studentNum;
int studentNumTwo;
String name;
String lname;
int classHour;
double grade;
int index = 0;
int indexTwo = 0;
double GPA;
double points;
String letterGrade;
//arrays
int[] studentNumArray = new int[20];
int[] studentNumTwoArray = new int[20];
String[] nameArray = new String[20];
String[] lnameArray = new String[20];
int[] classHourArray = new int[20];
double[] gradeArray = new double[20];
double[] GPAArray = new double[20];
String[] letterGradeArray = new String[20];
//first file
for (String numParse = buffReader.readLine(); numParse != null; numParse = buffReader.readLine()) {
studentNum = Integer.parseInt(numParse);
studentNumArray[index] = studentNum;
name = buffReader.readLine();
nameArray[index] = name;
lname = buffReader.readLine();
lnameArray[index] = lname;
index++;
}
//read file 2
for (String numTwoParse = buffReaderTwo.readLine(); numTwoParse != null; numTwoParse = buffReaderTwo.readLine()) {
studentNumTwo = Integer.parseInt(numTwoParse);
studentNumTwoArray[indexTwo] = studentNumTwo;
String classParse = buffReaderTwo.readLine();
classHour = Integer.parseInt(classParse);
classHourArray[indexTwo] = classHour;
String gradeParse = buffReaderTwo.readLine();
grade = Double.parseDouble(gradeParse);
gradeArray[indexTwo] = grade;
//GPA
points = grade * classHour;
GPA = points / classHour;
GPAArray[indexTwo] = GPA;
//Grade
if(GPA >= 4.0){
letterGrade="A";
letterGradeArray[indexTwo]=letterGrade;
}
if (GPA < 4.0 && GPA >= 3.4) {
letterGrade = "A";
letterGradeArray[indexTwo] = letterGrade;
}
if (GPA < 3.4 && GPA >= 2.7) {
letterGrade = "B";
letterGradeArray[indexTwo] = letterGrade;
}
if (GPA < 2.7 && GPA >= 2.0) {
letterGrade = "C";
letterGradeArray[indexTwo] = letterGrade;
}
if (GPA < 2.0 && GPA >= 1.2) {
letterGrade = "D";
letterGradeArray[indexTwo] = letterGrade;
}
if (GPA < 1.2 && GPA >= 0.1) {
letterGrade = "F";
letterGradeArray[indexTwo] = letterGrade;
}
if (GPA < 0.1) {
letterGrade = "NONE";
letterGradeArray[indexTwo] = letterGrade;
}
indexTwo++;
}
//final sort by GPA
double temp2;
int length2 = GPAArray.length;
for(int i = 0; i<length2; i++){
for(int j =i+1; j<length2; j++){
if(GPAArray[i]< GPAArray[j]){
temp2=GPAArray[i];
GPAArray[i]=GPAArray[j];
GPAArray[j]=temp2;
String temp6;
temp6=letterGradeArray[i];
letterGradeArray[i]=letterGradeArray[j];
letterGradeArray[j]=temp6;
int temp7;
temp7=studentNumTwoArray[i];
studentNumTwoArray[i]=studentNumTwoArray[j];
studentNumTwoArray[j]=temp7;
int temp3;
temp3=studentNumArray[i];
studentNumArray[i]=studentNumArray[j];
studentNumArray[j]=temp3;
}
}
}
studentNumArray=studentNumTwoArray.clone();
System.out.printf("%40s\n", "Student Report");
System.out.printf("%-20s %10s %15s %15s\n", "Student Number", "Student Name", "GPA", "Grade");
System.out.println("_______________________________________________________________________");
for (int i = 0; i < studentNumArray.length; i++) {
if (nameArray[i] != null) {
System.out.printf("%-18d %-1s %-18s %-20.2f %-10s\n",
studentNumTwoArray[i], nameArray[i], lnameArray[i], GPAArray[i], letterGradeArray[i]);
}
}
}
如果你愿意,请帮忙。