I'm having a bit of trouble formatting my code to give the correct output. I'm a beginner at Java, so I expect I've made some rather stupid mistakes, but here goes.
The text file that I'm inputting is:
John Smith 90
Barack Obama 95
Al Clark 80
Sue Taylor 55
Ann Miller 75
George Bush 58
John Miller 65
The output file looks a little something like this:
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
The output that I'm looking for is this:
The output file looks a little something like this:
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
The issues are:
I need to have the names (first and last) print the grades for the minimum and maximum. 牌号为最小和最大打印。
The descending order of the grades is obviously ascending, so that needs to be fixed.
I also need the names (first and last) to print with each grade in the organized descending list at the end, right now the names don't match up to the grades.
Here's my code:
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;
}
}
}
The issues are:
- I need to have the names (first and last) print with the grades for the minimum and maximum.
Keep two Strings
that each contain the name of the person with the min and max grade, just like you did with the actual min and max value.
- The descending order of the grades is obviously ascending, so that needs to be fixed.
It looks like you've already sorted your array so that it is in ascending order (Btw look in to Arrays.sort()
if you're allowed), so instead of printing it from front to back, why don't you print it starting from the end of your array? This should print it in descending order.
- I also need the names (first and last) to print with each grade in the organized descending list at the end.
Your grades aren't matching up with the correct person because you're switching the grades of each person in the array, instead of switching the People around in the array.
See here:
t=st[i].grade;
st[i].grade=st[i+1].grade;
st[i+1].grade=t;
Let's pretend you have 2 people, Bob and Sam, with grades of 50 and 75,respectively.
Here they are in the array: [Bob(50)][Sam(75)]
Using the above code to swap them, you would end up with this: [Bob(75)][Sam(50)]
The changes to fix this are trivial. I'll leave it to you to figure out.
okay, a quick and dirty example of what I would do:
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();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.