qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請(qǐng)?jiān)L問 http://qaseven.github.io/

          java io 根據(jù)TXT 在控制臺(tái)上輸出相關(guān)表的信息

          import java.io.BufferedReader;
          import java.io.File;
          import java.io.FileInputStream;
          import java.io.FileNotFoundException;
          import java.io.IOException;
          import java.io.InputStreamReader;
          import java.io.Serializable;
          import java.text.DecimalFormat;
          import java.util.ArrayList;
          import java.util.Collections;
          import java.util.Comparator;
          import java.util.List;
          public class TestCut {
          /**
          * @param args
          * @throws Exception
          */
          public static void main(String[] args) throws Exception {
          TestCut test = new TestCut();
          List<Student> list = test.readFile("D:\\Students.txt", "GBK");
          test.printAver(list);
          System.out.println("-------------------------------------------");
          test.printExcel(list);
          System.out.println("---------------------------------------------");
          test.printEnglishAvg(list);
          }
          /**
          * 按照平均分降序格式化輸出
          * @param list
          */
          public void printAver(List<Student> list) {
          if (list != null) {
          String top = "序號(hào)\t學(xué)號(hào)\t平均分\t數(shù)學(xué)\t語文\t英語";
          System.out.println(top);
          Collections.sort(list, new Comparator<Student>() {
          // 根據(jù)平均數(shù)來進(jìn)行比較,降序排序
          public int compare(Student arg0, Student arg1) {
          if (arg1 == null)
          return -1;
          if (arg0.getAvg() < arg1.getAvg())
          return 1;
          else if (arg0.getAvg() == arg1.getAvg())
          return 0;
          else
          return -1;
          }
          });
          for (int i = 0; i < list.size(); i++) {
          Student student = list.get(i);
          System.out.print((i + 1) + "\t" + student.getS_no() + "\t"
          + student.getAvg() + "\t");
          System.out.print(student.getMaths() + "\t"
          + student.getChinese() + "\t");
          if (student.getEnglish() != null)
          System.out.println(student.getEnglish());
          else {
          System.out.println();
          }
          }
          } else {
          System.out.println("文件內(nèi)容為空!");
          }
          }
          /**
          * 按照優(yōu)秀率格式化輸出
          * @param list
          */
          public void printExcel(List<Student> list) {
          if (list != null) {
          String top = "序號(hào)\t學(xué)號(hào)\t優(yōu)秀率\t數(shù)學(xué)\t語文\t英語";
          System.out.println(top);
          Collections.sort(list, new Comparator<Student>() {
          // 根據(jù)優(yōu)秀率來進(jìn)行比較,降序排序
          public int compare(Student arg0, Student arg1) {
          if (arg1 == null)
          return -1;
          if (arg0.getExcel() < arg1.getExcel())
          return 1;
          else if (arg0.getExcel() == arg1.getExcel())
          return 0;
          else
          return -1;
          }
          });
          for (int i = 0; i < list.size(); i++) {
          Student student = list.get(i);
          DecimalFormat df = new DecimalFormat("#%");
          System.out.print((i + 1) + "\t" + student.getS_no() + "\t"
          + df.format(student.getExcel()) + "\t");
          System.out.print(student.getMaths() + "\t"
          + student.getChinese() + "\t");
          if (student.getEnglish() != null)
          System.out.println(student.getEnglish());
          else {
          System.out.println();
          }
          }
          } else {
          System.out.println("文件內(nèi)容為空!");
          }
          }
          /**
          * 求英語平均成績
          * @param list
          */
          public void printEnglishAvg(List<Student> list) {
          printAvgByCourse(list, 3);
          }
          /**
          * 求課程平均成績,并輸出
          * @param list
          * @param course
          *        課程(1:數(shù)學(xué),2:語文,3:英語)
          */
          private void printAvgByCourse(List<Student> list, int course) {
          Integer avg = 0;
          switch (course) {
          case 1: {
          Integer maths = 0;
          for (Student student : list) {
          maths += student.getMaths();
          }
          avg = maths / list.size();
          System.out.println("數(shù)學(xué)平均成績:\t" + avg);
          break;
          }
          case 2: {
          Integer chinese = 0;
          for (Student student : list) {
          chinese += student.getChinese();
          }
          avg = chinese / list.size();
          System.out.println("語文平均成績:\t" + avg);
          break;
          }
          case 3: {
          Integer english = 0;
          Integer size = 0;
          for (Student student : list) {
          if (student.getEnglish() != null) {
          english += student.getEnglish();
          size++;
          }
          }
          if (size != 0)
          avg = english / size;
          System.out.println("英語平均成績:\t" + avg);
          break;
          }
          default: {
          System.out.println("不存在此課程");
          break;
          }
          }
          }
          /**
          * 讀取文件信息
          * @param fileName
          *            文件路徑
          * @param charset
          *            編碼
          * @return
          * @throws IOException
          */
          public List<Student> readFile(String fileName, String charset) {
          List<Student> list = new ArrayList<Student>();
          FileInputStream fi = null;
          BufferedReader in = null;
          try {
          fi = new FileInputStream(new File(fileName));
          in = new BufferedReader(new InputStreamReader(fi, charset));
          String result = in.readLine();
          while ((result = in.readLine()) != null) {
          String[] str = result.split("\t");
          Student student = new Student();
          for (int i = 0; i < str.length; i++) {
          student.setS_no(str[0]);
          student.setMaths(Integer.parseInt(str[1]));
          student.setChinese(Integer.parseInt(str[2]));
          if (str.length > 3) {
          student.setEnglish(Integer.parseInt(str[3]));
          }
          student.setAvg(student.culAvg());
          student.setExcel(student.culExcel());
          }
          list.add(student);
          }
          } catch (FileNotFoundException e) {
          e.printStackTrace();
          } catch (IOException e) {
          e.printStackTrace();
          }
          return list;
          }
          }
          class Student implements Serializable {
          private static final long serialVersionUID = -6517638655032546653L;
          /**
          * 學(xué)號(hào)
          */
          private String s_no;
          /**
          * 數(shù)學(xué)
          */
          private Integer maths;
          /**
          * 語文
          */
          private Integer chinese;
          /**
          * 英語
          */
          private Integer english;
          /**
          * 平均分
          */
          private Integer avg;
          /**
          * 優(yōu)秀率
          */
          private float excel;
          public Student() {
          }
          public Student(Integer maths, Integer chinese, Integer english) {
          this.maths = maths;
          this.chinese = chinese;
          this.english = english;
          this.avg = culAvg(maths, chinese, english);
          this.excel = culExcel(maths, chinese, english);
          }
          public String getS_no() {
          return s_no;
          }
          public void setS_no(String sNo) {
          s_no = sNo;
          }
          public Integer getMaths() {
          return maths;
          }
          public void setMaths(Integer maths) {
          this.maths = maths;
          }
          public Integer getChinese() {
          return chinese;
          }
          public void setChinese(Integer chinese) {
          this.chinese = chinese;
          }
          public Integer getEnglish() {
          return english;
          }
          public void setEnglish(Integer english) {
          this.english = english;
          }
          public Integer getAvg() {
          return avg;
          }
          public void setAvg(Integer avg) {
          this.avg = avg;
          }
          public float getExcel() {
          return excel;
          }
          public void setExcel(float excel) {
          this.excel = excel;
          }
          public String toString() {
          StringBuffer sb = new StringBuffer("[");
          sb.append("s_no=" + s_no).append(",maths=").append(maths).append(
          ",chinese=").append(chinese).append(",english=")
          .append(english).append(",avg=").append(avg).append(",excel=")
          .append(excel).append("]");
          return sb.toString();
          }
          /**
          * 計(jì)算平均數(shù)
          * @param maths
          *            數(shù)學(xué)
          * @param chinese
          *            語文
          * @param english
          *            英語
          * @return
          */
          private Integer culAvg(Integer maths, Integer chinese, Integer english) {
          // 計(jì)算平均分
          Integer sum = chinese + maths;
          float aver = english == null ? (float) sum / 2
          : (float) (sum + english) / 3;
          return Math.round(aver);
          }
          /**
          * 計(jì)算優(yōu)秀率
          * @param maths
          *            數(shù)學(xué)
          * @param chinese
          *            語文
          * @param english
          *            英語
          * @return
          */
          private float culExcel(Integer maths, Integer chinese, Integer english) {
          final Integer EXCEL_NUMBER = 85;
          Integer total_number = english == null ? 2 : 3;
          Integer ex = 0;
          if (maths >= EXCEL_NUMBER)
          ex++;
          if (chinese >= EXCEL_NUMBER)
          ex++;
          if (english != null && english >= EXCEL_NUMBER)
          ex++;
          return (float) ex / total_number;
          }
          /**
          * 計(jì)算平均數(shù)
          * @return
          */
          public Integer culAvg() {
          return culAvg(this.maths, this.chinese, this.english);
          }
          /**
          * 計(jì)算優(yōu)秀率
          * @return
          */
          public float culExcel() {
          return culExcel(this.maths, this.chinese, this.english);
          }
          }

          posted on 2011-09-21 22:13 順其自然EVO 閱讀(488) 評(píng)論(0)  編輯  收藏

          <2011年9月>
          28293031123
          45678910
          11121314151617
          18192021222324
          2526272829301
          2345678

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 藁城市| 太仓市| 林甸县| 安义县| 莱芜市| 铁力市| 宜春市| 连南| 宁南县| 鹤壁市| 轮台县| 莆田市| 赣榆县| 襄城县| 宁陵县| 巴彦县| 金华市| 松溪县| 句容市| 灵丘县| 广饶县| 阿荣旗| 深泽县| 河津市| 涿州市| 台湾省| 萝北县| 南岸区| 海林市| 大邑县| 崇文区| 韩城市| 安吉县| 纳雍县| 新竹市| 阜城县| 唐海县| 兴城市| 上饶市| 崇仁县| 林口县|