我有一个程序应该从文件中读取学生ID和GPA,将它们放在2个单独的数组中,按GPA范围对学生进行分类,对这种分类进行直方图,最后根据GPA(联系关系)进行排名),但仍按文件中的顺序打印它们。
我想我已经弄清楚了程序的每个部分,但是有一个问题。我不知道如何仅获取关联的学生ID和GPA的等级来与它们一起打印。相反,我只能一遍又一遍地打印一行,其中包含每个学生/ GPA的排名。
输出示例:
S8887184
3.2
[228、835、655、774、579、602、873、884、966、592、708、865 ...等)
所需的输出:
S8887184
3.2
228
在第一行中的输出中,第一行是学生ID,第二行是GPA,第三行是其他学生的排名。如果我还可以采用一种方法来表明等级是平局,以及与该排名并列的其他学生数量,那也将是理想的。
下面是我的代码供参考。如果您能帮助我解决此问题,将不胜感激!谢谢!
public static void main(String[] args) throws Exception
{
Scanner gpadata;
String snum;
double gpa;
String[] IDs = new String[1000];
double[] GPAs = new double[1000];
int counter;
counter = 0;
int[] gpaGroup = new int[8];
gpadata = new Scanner(new File("studentdata.txt"));
while (gpadata.hasNext())
{
snum = gpadata.next();
gpa = gpadata.nextDouble();
IDs[counter] = snum;
GPAs[counter] = gpa;
//Group students by GPA range
if (GPAs[counter] >= 0.0 && GPAs[counter] < 0.5)
gpaGroup[0]++;
else if (GPAs[counter] >= 0.5 && GPAs[counter] < 1.0)
gpaGroup[1]++;
else if (GPAs[counter] >= 1.0 && GPAs[counter] < 1.5)
gpaGroup[2]++;
else if (GPAs[counter] >= 1.5 && GPAs[counter] < 2.0)
gpaGroup[3]++;
else if (GPAs[counter] >= 2.0 && GPAs[counter] < 2.5)
gpaGroup[4]++;
else if (GPAs[counter] >= 2.5 && GPAs[counter] < 3.0)
gpaGroup[5]++;
else if (GPAs[counter] >= 3.0 && GPAs[counter] < 3.5)
gpaGroup[6]++;
else
gpaGroup[7]++;
counter++;
}
//Round number of students in each GPA group to nearest 10
int histogram = Math.round(gpaGroup[0]/10);
int histogram1 = Math.round(gpaGroup[1]/10);
int histogram2 = Math.round(gpaGroup[2]/10);
int histogram3 = Math.round(gpaGroup[3]/10);
int histogram4 = Math.round(gpaGroup[4]/10);
int histogram5 = Math.round(gpaGroup[5]/10);
int histogram6 = Math.round(gpaGroup[6]/10);
int histogram7 = Math.round(gpaGroup[7]/10);
//Print out GPA group, number of students in that group, and histogram
System.out.println("GPA Range # Histogram");
System.out.println("0.00 to 0.49 " + gpaGroup[0] + " " +
toStars(histogram));
System.out.println("0.50 to 0.99 " + gpaGroup[1] + " " +
toStars(histogram1));
System.out.println("1.00 to 1.49 " + gpaGroup[2] + " " +
toStars(histogram2));
System.out.println("1.50 to 1.99 " + gpaGroup[3] + " " +
toStars(histogram3));
System.out.println("2.00 to 2.49 " + gpaGroup[4] + " " +
toStars(histogram4));
System.out.println("2.50 to 2.99 " + gpaGroup[5] + " " +
toStars(histogram5));
System.out.println("3.00 to 3.49 " + gpaGroup[6] + " " +
toStars(histogram6));
System.out.println("3.50 to 4.00 " + gpaGroup[7] + " " +
toStars(histogram7));
//Add blank lines between histogram and part 2
System.out.println();
System.out.println();
//print rank
System.out.println("Student ID Number, GPA, and Class Rank");
System.out.println();
for (int k=0; k < IDs.length; k++){
System.out.println(IDs[k]);
System.out.println(GPAs[k]);
System.out.println(Arrays.toString(getRanksArray(GPAs)));
System.out.println();
k++;
}
}
//Method to convert rounded # of students to histogram
public static String toStars(int number)
{
StringBuilder temp = new StringBuilder();
for(int i=0; i<number; i++){
temp.append("*");
}
return temp.toString();
}
//Method to determine students class rank
public static int[] getRanksArray(double[] array)
{
int[] result = new int[array.length];
for (int i = 0; i < array.length; i++) {
int count = 0;
for (int j = 0; j < array.length; j++) {
if (array[j] > array[i]) {
count++;
}
}
result[i] = count + 1;
}
return result;
}
最佳答案
我建议将GPA计数包装在一个对象中,以便可以将索引与计数相关联。
class GpaCount {
private int count = 0;
private final int index;
public GpaCount(int index) {
this.index = index;
}
public int getCount() {
return count;
}
public void increment() {
count++;
}
public int getIndex() {
return index;
}
}
然后,您可以使用自定义
Collections.sort()
使用Comparator
对计数进行排序:List<GpaCount> gpaCounts = new ArrayList<>();
// populate gpaCount (insert your GPA counting logic here)
Comparator<GpaCount> comparator = (GpaCount o1, GpaCount o2) -> Integer.compare(o1.getCount(), o2.getCount());
Collections.sort(gpaCounts, comparator);
// now gpaCounts is sorted by count
关于java - 对数组进行排序(不排序或创建对象)并保留原始索引,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58698984/