A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© bjuth 中级黑马   /  2015-1-7 18:26  /  966 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

这是我的代码:
  1. package com.success;

  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.InputStreamReader;
  5. import java.io.PrintStream;
  6. import java.util.Comparator;
  7. import java.util.Date;
  8. import java.util.TreeSet;

  9. /*有五个学生,每个学生有3门课(语文、数学、英语)的成绩,写一个程序接收从键盘输入学生的信息,
  10. 输入格式为:name,30,30,30(姓名,三门课成绩),
  11. 然后把输入的学生信息按总分从高到低的顺序写入到一个名称"stu.txt"文件中。
  12. 要求:stu.txt文件的格式要比较直观,打开这个文件,就可以很清楚的看到学生的信息。*/
  13. public class Success12 {
  14.         public static void main(String[] args) throws Exception {
  15.                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  16.                 TreeSet<Student> tm = new TreeSet<Student>(new Comp());

  17.                 File f = new File("d:\\" + new Date().getTime() + ".txt");

  18.                 String line = null;
  19.                 String[] arr = null;
  20.                 while ((line = br.readLine()) != null) {
  21.                         if ("".equals(line))
  22.                                 break;
  23.                         arr = line.split(",");
  24.                         tm.add(new Student(arr[0], arr[1], arr[2], arr[3]));
  25.                 }
  26.                 br.close();

  27.                 // 获取名字和成绩的最大长度
  28.                 int nameMaxLen = 0, scoreMaxLen = 0;
  29.                 for (Student s : tm) {
  30.                         if (s.getName().length() > nameMaxLen)
  31.                                 nameMaxLen = s.getName().length();
  32.                         if (s.getYw().length() > scoreMaxLen)
  33.                                 scoreMaxLen = s.getYw().length();
  34.                         if (s.getSx().length() > scoreMaxLen)
  35.                                 scoreMaxLen = s.getSx().length();
  36.                         if (s.getYy().length() > scoreMaxLen)
  37.                                 scoreMaxLen = s.getYy().length();
  38.                 }
  39.                 System.out.println("nameMaxLen:" + nameMaxLen);
  40.                 System.out.println("scoreMaxLen:" + scoreMaxLen);

  41.                 System.setOut(new PrintStream(f));// 设置默认输出设备为文件f

  42.                 //按格式打印到文件f中
  43.                 System.out.printf("%" + (nameMaxLen * 2 - 2) + "s%" + (scoreMaxLen + 1)
  44.                                 + "s%" + (scoreMaxLen + 1) + "s%" + (scoreMaxLen + 1) + "s",
  45.                                 "名字", "语文", "数学", "英语");
  46.                 System.out.println();
  47.                 for (Student s : tm) {
  48.                         System.out
  49.                                         .printf("%" + (nameMaxLen) + "s%" + (scoreMaxLen + 3)
  50.                                                         + "s%" + (scoreMaxLen + 3) + "s%"
  51.                                                         + (scoreMaxLen + 3) + "s", s.getName(), s.getYw(),
  52.                                                         s.getSx(), s.getYy());
  53.                         System.out.println();
  54.                 }
  55.         }
  56. }

  57. class Student {
  58.         String name;
  59.         String yw;
  60.         String sx;
  61.         String yy;

  62.         Student(String name, String yw, String sx, String yy) {
  63.                 this.name = name;
  64.                 this.yw = yw;
  65.                 this.sx = sx;
  66.                 this.yy = yy;
  67.         }

  68.         public String getName() {
  69.                 return name;
  70.         }

  71.         public void setName(String name) {
  72.                 this.name = name;
  73.         }

  74.         public String getYw() {
  75.                 return yw;
  76.         }

  77.         public void setYw(String yw) {
  78.                 this.yw = yw;
  79.         }

  80.         public String getSx() {
  81.                 return sx;
  82.         }

  83.         public void setSx(String sx) {
  84.                 this.sx = sx;
  85.         }

  86.         public String getYy() {
  87.                 return yy;
  88.         }

  89.         public void setYy(String yy) {
  90.                 this.yy = yy;
  91.         }
  92. }

  93. class Comp implements Comparator<Student> {
  94.         @Override
  95.         public int compare(Student s1, Student s2) {
  96.                 int sum1 = (int) (Double.parseDouble(s1.getYw())
  97.                                 + Double.parseDouble(s1.getSx()) + Double.parseDouble(s1
  98.                                 .getYy()));
  99.                 int sum2 = (int) (Double.parseDouble(s2.getYw())
  100.                                 + Double.parseDouble(s2.getSx()) + Double.parseDouble(s2
  101.                                 .getYy()));
  102.                 return new Integer(sum2).compareTo(new Integer(sum1));
  103.         }
  104. }
复制代码
可以看到我是默认 学生姓名 都是汉字
  1. nameMaxLen * 2
复制代码

所以如果 学生姓名 含英文就会引起格式错乱
不知道该怎么修改??


捕获1.JPG (23.99 KB, 下载次数: 3)

捕获1.JPG

捕获2.JPG (35.86 KB, 下载次数: 0)

捕获2.JPG

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马