gpt4 book ai didi

java - 对数组进行排名(不排序或创建对象)并保留原始索引

转载 作者:行者123 更新时间:2023-12-01 22:04:28 24 4
gpt4 key购买 nike

我有一个程序,应该从文件中读取学生 ID 和 GPA,将它们放入 2 个单独的数组中,按 GPA 范围对学生进行分类,制作此分类的直方图,最后根据 GPA 对它们进行排名(考虑到领带),但仍然按照它们在文件中的顺序打印它们。

我想我已经弄清楚了程序的每一部分,但我有一个问题。我不知道如何仅获取相关学生证和 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()对计数进行排序:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com