0

我在格式化我的代码以提供正确的输出时遇到了一些麻烦。我是 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

问题是:

  1. 我需要打印姓名(名字和姓氏)以及最低和最高等级。

  2. 成绩的降序显然是升序,所以需要固定。

  3. 我还需要在最后有组织的降序列表中打印每个等级的名称(第一个和最后一个),现在这些名称与等级不匹配。


这是我的代码:

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;
    }


}
}
4

2 回答 2

3

问题是:

  1. 我需要打印姓名(名字和姓氏)以及最低和最高等级。

保留两个Strings,每个都包含具有最低和最高等级的人的姓名,就像你对实际的最小值和最大值所做的那样。

  1. 成绩的降序显然是升序,所以需要固定。

看起来您已经对数组进行了排序,使其按升序排列(顺便说一句,Arrays.sort()如果允许的话,请查看),所以不要从前到后打印它,为什么不从最后开始打印它你的阵列?这应该按降序打印。

  1. 我还需要在最后有组织的降序列表中打印每个等级的名称(第一个和最后一个)。

您的成绩与正确的人不匹配,因为您正在切换数组中每个人的成绩,而不是切换数组中的人。

看这里:

t=st[i].grade; 
st[i].grade=st[i+1].grade; 
st[i+1].grade=t;

假设您有两个人 Bob 和 Sam,他们的成绩分别为 50 和 75。

它们在数组中:[Bob(50)][Sam(75)]

使用上面的代码交换它们,你最终会得到:[Bob(75)][Sam(50)]

解决此问题的更改是微不足道的。我会留给你弄清楚。

于 2012-12-24T15:50:16.177 回答
0

好吧,我会做一个快速而肮脏的例子:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Students {

List<Student> students = new ArrayList<Student>();

public Students() {
    // create quick list, no reading from file, don't use arrays if you can help it
    students.add(new Student("a", 1));
    students.add(new Student("b", 2));
    students.add(new Student("c", 3));

    // sort list descending
    Collections.sort(students, new Comparator<Student>() {
        @Override
        public int compare(Student student1, Student student2) {
            return -1 * student1.grade.compareTo(student2.grade);
        }
    });
}

public void showDescending() {
    // just so the list that was sorted descending
    System.out.println(students);
}

public void showForRange(Integer minGrade, Integer maxGrade) {
    // show all students with grades between minGrade and maxGrade
    for (Student s : students) {
        if (s.grade >= minGrade && s.grade <= maxGrade) {
            System.out.println(s);
        }
    }
}

public class Student {
    String name;
    Integer grade;

    public Student(String name, Integer grade) {
        this.name = name;
        this.grade = grade;
    }

    // use toString to easily show name with grade for each student in list
    @Override
    public String toString() {
        return name + " " + grade;
    }

}

public static void main(String[] args) {
    Students s = new Students();
    s.showForRange(100, 90);
    s.showForRange(89, 80);
    s.showDescending();

}


}
于 2012-12-24T16:09:43.213 回答