【问题标题】:Reading from a txt file, sorting, and outputing to another txt file从一个txt文件读取,排序,输出到另一个txt文件
【发布时间】:2013-11-25 03:34:34
【问题描述】:

我希望有人会花时间帮助我。我是 Java 新手,正在上课尝试学习它。我有一个任务,我已经开始并删除了大约 30 次。这个只是没有点击我。作业如下:

从 txt 文件中读取 3 列整数。一栏是学生编号,一栏是作业的分数,第三栏是该作业的最大可能分数。 (有 5 名学生,每人 10 分)。

我必须使用至少 1 个数组。

列出从作业中获得的总分,以及 5 名学生每人在 10 项作业中可能获得的最高分。然后将学生编号、分数、作业的最大可能分数、百分比成绩以及最后的学生字母成绩输出到不同的 txt 文件。

txt 文件如下所示:

12345 10 15
12345 25 25
12345 89 100
12345 23 25
12345 9 10
12345 42 50
12345 75 100
12345 92 100
12345 40 50
12345 48 50
23456 12 15
23456 23 25
23456 99 100
23456 24 25
23456 9 10
23456 47 50
23456 88 100
23456 95 100
23456 35 50
23456 45 50
34567 10 15
34567 24 25
34567 87 100
34567 23 25
34567 9 10
34567 45 50
34567 97 100
34567 98 100
34567 38 50
34567 48 50
45678 8 15
45678 21 25
45678 78 100
45678 22 25
45678 9 10
45678 46 50
45678 76 100
45678 92 100
45678 37 50
45678 39 50
56789 8 15
56789 21 25
56789 78 100
56789 21 25
56789 8 10
56789 42 50
56789 72 100
56789 88 100
56789 32 50
56789 34 50

这是我目前所拥有的:

import java.awt.List;    
import java.io.FileNotFoundException;    
import java.io.FileReader;    
import java.util.ArrayList;    
import java.util.Arrays;    
import java.util.Scanner;    
import java.util.IOException;

public class gradesSummary {
    public static void main(String[] args) throws FileNotFoundException {
        ArrayList<Integer> l1 = new ArrayList<Integer>(1);
        ArrayList<Integer> l2 = new ArrayList<Integer>(2);
        ArrayList<Integer> l3 = new ArrayList<Integer>(3);


        Scanner s = new Scanner(new FileReader("grades.txt")); 

        while (s.hasNext()) {
            l1.add(s.nextInt());
            l2.add(s.nextInt());
            l3.add(s.nextInt());
        }

        s.close();

        System.out.println(l1);  
        System.out.println(l2);  
        System.out.println(l3);
    }   
}

我从这里去哪里?

我已经完成了大部分代码。我现在需要打印到一个新的文本文件。这是我的新代码:

公开课成绩总结{ public static void main(String[] args) 抛出 FileNotFoundException {

     int x = 1;
        Scanner inputFile = new Scanner(new File("grades.txt"));
        int[] studentNum = new int[100];
        int[] score = new int[100];
        int[] maxScore = new int[100];
        int[] totalScore = new int[6];
        int[] totalMaxScore = new int[6];
        int[] studentNumber = new int[6];
        double[] percentage = new double[6];
        //int[] totalScore = new int[100];
        //int[] totalMaxScore = new int[100];

        //int totalMaxScore = 0;
        //int totalScore = 0;
        //double percentage = 0.00;
        studentNum[0] = 0;
        while(inputFile.hasNext()){
                studentNum[x] = inputFile.nextInt();
                score[x] = inputFile.nextInt();
                maxScore[x] = inputFile.nextInt();
                x++;            
        }
        int y = 0;
        studentNumber[y] = studentNum[1]; 
        for (x = 1; x < 100; x++){
            if (x > 1 && studentNum[x] != studentNum[x-1]){
                percentage[y] = (double)totalScore[y] / (double)totalMaxScore[y];
                y++;
                studentNumber[y] = studentNum[x];
            }

            totalMaxScore[y] += maxScore[x];
            totalScore[y] += score[x];
        }

        for (int z = 0; z<= 4; z++){
            System.out.println(z + " " +studentNumber[z]+" "+percentage[z]);
        }
      }

}
//inputFile.close();

对于创建一个新的文本文件来打印 studentNum、totalscore、totalmaxscore、百分比成绩和字母成绩有什么建议吗?

感谢您提供的任何帮助。

【问题讨论】:

  • 如果你要求别人为你写代码,它可能会破坏你的学习过程。网上有很好的教程,先学做几个例子,然后回到这个例子尝试解决,如果你仍然遇到任何问题,那么只需将你的错误信息复制粘贴到这里,肯定会有人帮助你那
  • 你至少有一个算法来解决这个问题吗?
  • 谢谢。有什么特别的教程可以推荐吗?
  • 我已经做到了这一点,但需要指导才能将其写入新的文本文件。这是我的新代码:

标签: java


【解决方案1】:

执行此操作的 OOP 方法是创建一个 Student 类,其中包含 int id 和一个作业分数数组。请注意,每个学生都以相同的顺序收到相同的作业,因此您可以创建一个包含 10 个最高分数的数组并存储总和。

【讨论】:

    【解决方案2】:

    这样做

    你的数据

        ArrayList<Integer[]> list = new ArrayList<Integer[]>();
        Scanner s = new Scanner(new FileReader("grades.txt")); 
        while (s.hasNext()) {
            Integer[] array= new Integer[3];
            array[0]=s.nextInt();
            array[1]=s.nextInt();
            array[2]=s.nextInt();
            list.add(array);
        }
        s.close();
    

    你的比较器

    class SimpleComparator implements Comparator<Integer[]> {    
        @Override
        public int compare(Integer[] o1, Integer[] o2) {
            if(!o1[0].equals(o2[0])){
                return o1[0].compareTo(o2[0]);
            }
            if(!o1[1].equals(o2[1])){
                return o1[1].compareTo(o2[1]);
            }
            if(!o1[2].equals(o2[2])){
                return o1[2].compareTo(o2[2]);
            }
            return 0;
        }        
    }
    

    你的排序

      Collections.sort(list,new SimpleComparator());
    

    【讨论】:

    • 非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-29
    • 2016-06-18
    • 2020-02-21
    相关资源
    最近更新 更多