Java 按递减顺序打印文件内容
我需要写一个Java程序,在那里我可以从一个有学生姓名和成绩的文件中读取数据。我必须找到每个学生的最高成绩,然后把他的名字和最高成绩写在一个新文件里。我已经能够做到这一点。问题是,学生应该根据他们的最高成绩按降序打印,我不知道怎么做。你能帮我吗?谢谢大家!Java 按递减顺序打印文件内容,java,Java,我需要写一个Java程序,在那里我可以从一个有学生姓名和成绩的文件中读取数据。我必须找到每个学生的最高成绩,然后把他的名字和最高成绩写在一个新文件里。我已经能够做到这一点。问题是,学生应该根据他们的最高成绩按降序打印,我不知道怎么做。你能帮我吗?谢谢大家! public class NotaMax { public static void main(String args[]) throws FileNotFoundException{ Scanner input=new
public class NotaMax {
public static void main(String args[]) throws FileNotFoundException{
Scanner input=new Scanner(new File("teksti.txt"));
PrintStream output=new PrintStream(new File("max.txt"));
while(input.hasNextLine()) {
String rreshti=input.nextLine();
max(rreshti,output);
}
}
public static void max(String text,PrintStream output) {
Scanner data=new Scanner(text);
String emri=data.next();
double max=0;
while(data.hasNext()) {
double nota=data.nextDouble();
if(nota>max) {
max=nota;
}
}
output.println(""+emri+":"+max);
}
}
这有两种方法,一种是填充另一种 您可以将它们保存在
ArrayList
中,然后调用方法Array#reverse
,这样它将反转ArrayList
。为了增加另一层确定性,最好将对象
/类
命名为学生
,并将比较器
应用于数组列表
的排序
方法,以确保结果。
然而,这比解决这个问题的最简单和最有效的方法需要更多的步骤
您最好将学生对象
保存在数组列表
(或哈希集
,实际上是任何可比较的集合/映射
)中,并使用#sort
方法从下到上排序。
我可以(如果要求的话)为此提供一些代码。有两种方法,一种是填充另一种 您可以将它们保存在
ArrayList
中,然后调用方法Array#reverse
,这样它将反转ArrayList
。为了增加另一层确定性,最好将对象
/类
命名为学生
,并将比较器
应用于数组列表
的排序
方法,以确保结果。
然而,这比解决这个问题的最简单和最有效的方法需要更多的步骤
您最好将学生对象
保存在数组列表
(或哈希集
,实际上是任何可比较的集合/映射
)中,并使用#sort
方法从下到上排序。
我可以(如果要求的话)为此提供一些代码。我想到的第一件事是为您的学生创建一个单独的类,并实现一个简单的二进制堆作为最大堆,每个学生的最大分数作为排序标准。然后把它打印出来。不应该太难 我想到的第一件事是为您的学生创建一个单独的类,并实现一个简单的二进制堆作为最大堆,每个学生的最大分数作为排序标准。然后把它打印出来。不应该太难 这应该能解决你的问题。首先阅读地图中的学生和等级,然后找到每个学生的最高等级
public class NotaMax {
private static Map<String, ArrayList<Integer>> students = new HashMap<>();
private static void readTeksti() throws FileNotFoundException {
Scanner input = new Scanner(new File("teksti.txt"));
// read teksti.txt
String[] line;
while (input.hasNextLine()) {
String rreshti = input.nextLine();
line = rreshti.split(" ");
ArrayList<Integer> grades = students.get(line[0]);
if (grades == null) {
grades = new ArrayList<>();
grades.add(Integer.parseInt(line[1]));
students.put(line[0], grades);
} else {
grades.add(Integer.parseInt(line[1]));
}
}
}
public static void main(String args[]) throws FileNotFoundException {
readTeksti();
max();
}
private static void max() throws FileNotFoundException {
PrintStream output = new PrintStream(new File("max.txt"));
List<Map.Entry<String,Integer>> maximums = new LinkedList<>();
for (String current : students.keySet()) {
ArrayList<Integer> grades = students.get(current);
Integer max = Collections.max(grades);
maximums.add(new AbstractMap.SimpleEntry<>(current, max));
}
Collections.reverse(maximums);
for (Map.Entry<String,Integer> current : maximums) {
output.println("" + current.getKey() + ":" + current.getValue());
}
}
}
这应该能解决你的问题。首先阅读地图中的学生和等级,然后找到每个学生的最高等级
public class NotaMax {
private static Map<String, ArrayList<Integer>> students = new HashMap<>();
private static void readTeksti() throws FileNotFoundException {
Scanner input = new Scanner(new File("teksti.txt"));
// read teksti.txt
String[] line;
while (input.hasNextLine()) {
String rreshti = input.nextLine();
line = rreshti.split(" ");
ArrayList<Integer> grades = students.get(line[0]);
if (grades == null) {
grades = new ArrayList<>();
grades.add(Integer.parseInt(line[1]));
students.put(line[0], grades);
} else {
grades.add(Integer.parseInt(line[1]));
}
}
}
public static void main(String args[]) throws FileNotFoundException {
readTeksti();
max();
}
private static void max() throws FileNotFoundException {
PrintStream output = new PrintStream(new File("max.txt"));
List<Map.Entry<String,Integer>> maximums = new LinkedList<>();
for (String current : students.keySet()) {
ArrayList<Integer> grades = students.get(current);
Integer max = Collections.max(grades);
maximums.add(new AbstractMap.SimpleEntry<>(current, max));
}
Collections.reverse(maximums);
for (Map.Entry<String,Integer> current : maximums) {
output.println("" + current.getKey() + ":" + current.getValue());
}
}
}
您可以将所有记录放在ArrayList中,然后使用Collections界面的排序方法对它们进行排序。如果可能,您可以帮助我编写一些代码,因为我不熟悉this@m_lat我用一些代码写了一个答案,如果您需要额外的解释,请对该答案进行评论。您可以将所有记录放入ArrayList中,然后使用Collections界面的排序方法对其进行排序。如果可能,您可以帮助我编写一些代码,因为我不熟悉this@m_lat我用一些代码写了一个答案,如果你需要额外的解释,请对答案发表评论。如果可能,你能帮我一些代码吗?因为我不熟悉这个,你能帮我一些代码吗?如果可能,因为我不熟悉这个,你能帮我一些代码吗,我会在添加代码后发表评论。如果可能的话,你能帮我写一些代码吗?因为我不熟悉这个。我会用代码编辑我的帖子,所以它会出现。我添加代码后会发表评论。