資料檔:

國文,英文,管理學,程設,經濟學,
3,2,3,3,2,
60,61,62,63,64,
70,71,72,73,74,
85,90,95,80,70,

 

-------------

 

package chap1;

import java.io.*;
 
public class test1218 {

  public static void main(String[] args) throws Exception {
   BufferedReader fr = new BufferedReader(new FileReader("data.txt"));
   int creditsum = 0, avg = 0, total = 0, k = 0, i = 0, j = 0;
   int num = 0, score[] = new int[20], credit[] = new int[20];
   String str = "", str1 = "", subjectname[] = new String[20];

   str = fr.readLine();
   for (i = 0; i < str.length(); i++) {
    if (str.charAt(i) != ',') {
     str1 = str1 + str.charAt(i);
    } else {
     subjectname[j] = str1;
     System.out.print(subjectname[j]+"\t");
     j++;
     str1 = "";
    }
   }
   System.out.println();
   j = 0;
   str = fr.readLine();
   for (i = 0; i < str.length(); i++) {
    if (str.charAt(i) != ',') {
     num = num * 10 + str.charAt(i) - 48;
    } else {
     credit[j] = num;
     num = 0;
     System.out.print(credit[j] + "\t");
     j++;
    }
   }
   for (k = 0; k < credit.length; k++) {
   creditsum = creditsum + credit[k];
   }
   while ((str = fr.readLine()) != null) {
    System.out.println();
    j = 0;
    total=0;
    for (i = 0; i < str.length(); i++) {
     if (str.charAt(i) != ',') {
      num = num * 10 + str.charAt(i) - 48;
     } else {
      score[j] = num;
      num = 0;
      System.out.print(score[j] + "\t");
      j++;
     }
    }

    for (k = 0; k < score.length; k++) {
     total = total + score[k] * credit[k];
     
    }
    avg = total / creditsum;
    System.out.print("學期成績" + avg);
   }
  }
 }

 

 

arrow
arrow
    全站熱搜

    玥君 發表在 痞客邦 留言(0) 人氣()