我在格式化我的代码以提供正确的输出时遇到了一些麻烦。我是 Java 的初学者,所以我希望我犯了一些相当愚蠢的错误,但这里是这样。
我输入的文本文件是:
John Smith 90
Barack Obama 95
Al Clark 80
Sue Taylor 55
Ann Miller 75
George Bush 58
John Miller 65
输出文件看起来有点像这样:
John Smith 90
Barack Obama 95
Al Clark 80
Sue Taylor 55
Ann Miller 75
George Bush 58
John Miller 65
Students with excellent grades:
John Smith 90
Barack Obama 95
Students with ok grades:
Al Clark 80
Ann Miller 75
John Miller 65
Students with failure grades:
Sue Taylor 55
George Bush 58
Lowest Grade: Sue Taylor 55
Highest Grade: Barack Obama 95
Average of Grades: 74
Grades in descending order:
John Smith 55
Barack Obama 58
Al Clark 65
Sue Taylor 75
Ann Miller 80
George Bush 90
John Miller 95
我正在寻找的输出是这样的:
输出文件看起来有点像这样:
John Smith 90
Barack Obama 95
Al Clark 80
Sue Taylor 55
Ann Miller 75
George Bush 58
John Miller 65
Students with excellent grades:
John Smith 90
Barack Obama 95
Students with ok grades:
Al Clark 80
Ann Miller 75
John Miller 65
Students with failure grades:
Sue Taylor 55
George Bush 58
Lowest Grade: Sue Taylor 55
Highest Grade: Barack Obama 95
Average of Grades: 74
Grades in descending order:
Barack Obama 95
John Smith 90
Al Clark 80
Ann Miller 75
John Miller 65
George Bush 58
Sue Taylor 55
问题是:
我需要打印姓名(名字和姓氏)以及最低和最高等级。
成绩的降序显然是升序,所以需要固定。
我还需要在最后有组织的降序列表中打印每个等级的名称(第一个和最后一个),现在这些名称与等级不匹配。
这是我的代码:
import java.util.Scanner;
import java.io.*;
public class Students
{
public static void main (String[] args) throws IOException
{
String first_name, last_name;
int grade, total=0, count=0;
int min, max;
double average;
int excellentTotal = 0;
int okTotal = 0;
int failureTotal = 0;
Scanner fileInput = new Scanner(new File("students.txt"));
Student st[] = new Student [100];
while (fileInput.hasNext())
{
first_name = fileInput.next();
last_name = fileInput.next();
grade = fileInput.nextInt();
st[count] = new Student(first_name, last_name, grade);
count++;
total = total + grade;
}
for (int i=0; i<count;i++)
{
System.out.println(st[i]);
}
System.out.println("\nStudents with excellent grades:");
for (int i=0; i<count;i++)
{
if (st[i].grade > 89)
{
System.out.println(st[i]);
excellentTotal += st[i].grade;
}
}
System.out.println("\nStudents with ok grades:");
for (int i=0; i<count;i++)
{
if (st[i].grade >=60 && st[i].grade <=89)
{
System.out.println(st[i]);
okTotal += st[i].grade;
}
}
System.out.println("\nStudents with failure grades:");
for (int i=0; i<count;i++)
{
if (st[i].grade < 60)
{
System.out.println(st[i]);
failureTotal += st[i].grade;
}
}
min = st[0].getGrade();
max = st[0].getGrade();
for (int i = 0; i < count; i++)
{
if (max.grade < st[i].grade)
{
max = st[i];
}
if (min.st[i] > st[i].grade)
{
min = st[i].grade;
}
total = excellentTotal + okTotal + failureTotal;
}
System.out.println();
System.out.println("Lowest Grade: " + min);
System.out.println("Highest Grade: " + max);
System.out.println("Average of Grades: " + total/count);
int t, swap = 0;
do
{
swap = 0;
for (int i=0; i<count-1; i++)
{
if (st[i].getGrade()>st[i+1].getGrade())
{
t=st[i].grade;
st[i].grade=st[i+1].grade;
st[i+1].grade=t;
swap++;
}
}
}
while (swap>0);
System.out.println("\nGrades in descending order: ");
for (int i=0; i<count;i++)
{
System.out.print (st[i] + " ");
System.out.println ();
}
}
static class Student
{
private String fname, lname;
private int grade;
public Student(String fname, String lname, int grade)
{
this.fname = fname;
this.lname = lname;
this.grade = grade;
}
public int getGrade()
{
return grade;
}
public void setGrade(int grade)
{
this.grade = grade;
}
public String toString()
{
return fname + " " + lname + "\t" + grade;
}
}
}