簡體   English   中英

從txt文件讀取,排序並輸出到另一個txt文件

[英]Reading from a txt file, sorting, and outputing to another txt file

我希望有人花時間幫助我。 我是Java的新手,正在上一堂課來嘗試和學習它。 我有一個作業已經開始並已刪除了30次。 這只是不與我點擊。 分配如下:

從txt文件讀取3列整數。 一欄有學生編號,一欄是作業的分數,第三欄是該作業的最大可能分數。 (有5名學生,每人10分)。

我必須至少使用1個數組。

列出從作業中獲得的總分,並列出10個作業中5名學生中每人可能獲得的最高分。 然后將學生編號,分數,作業的最大可能分數,分數等級以及學生字母等級輸出到另一個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);
    }   
}

我從這里去哪里?

我已經完成了大部分代碼。 我現在需要打印到新的文本文件。 這是我的新代碼:

公共類GradesSummary {公共靜態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,百分比等級和字母等級的建議?

謝謝你提供的所有幫助。

OOP的方法是創建一個Student類,該類具有int id和一系列的作業分數。 請注意,每個學生都以相同的順序收到相同的作業,因此您可以創建10個最高分數的數組並存儲總和。

像這樣

您的數據

    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());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM