我在格式化代码时遇到了一些麻烦,无法提供正确的输出。我是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
问题是:
我需要将名称(第一个和最后一个)打印 with 最低和最高等级。
等级的降序显然是递增的,所以 需要修复。
我还需要在每个年级打印的名称(第一个和最后一个) 最后有组织的降序列表,现在名称与成绩不符。
这是我的代码:
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;
}
}
}
答案 0 :(得分:3)
问题是:
- 我需要打印等级的名称(第一个和最后一个) 最小和最大。
醇>
保留两个Strings
,每个Arrays.sort()
包含具有最低和最高等级的人的姓名,就像您使用实际的最小值和最大值一样。
- 等级的降序显然是递增的,所以 需要修复。
醇>
看起来你已经对数组进行了排序,使其按升序排列(如果允许的话,请顺便查看t=st[i].grade;
st[i].grade=st[i+1].grade;
st[i+1].grade=t;
),所以不要从前到后打印,为什么不要'你从阵列的末尾开始打印它?这应该按降序打印。
- 我还需要在每个年级打印的名称(第一个和最后一个) 最后组织了降序列表。
醇>
您的成绩与正确的人不匹配,因为您正在切换阵列中每个人的成绩,而不是在阵列中切换人员。
见这里:
[Bob(50)][Sam(75)]
让我们假装你有两个人,Bob和Sam,分别为50和75.
这里他们在数组中:[Bob(75)][Sam(50)]
使用上面的代码交换它们,最终会得到:{{1}}
修复此问题的更改非常简单。我会留给你弄清楚的。
答案 1 :(得分: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();
}
}