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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 朝花夕拾 中级黑马   /  2012-12-2 19:23  /  2160 人查看  /  9 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 张雄 于 2012-12-3 22:34 编辑

有如下代码:Student类已知,且具有该代码中调用的所有属性!
package cn.itcast;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.TreeSet;
/*
* 有三个学生,每个学生有3门课的成绩,从键盘输入以上数据(姓名,三门课成绩),
* 按照总成绩的排序,最后把排序后的数据写入文件。
*
* 思路:
* A:定义学生类
* B:录入3个学生,把学生存储到TreeSet集合。
* C:对TreeSet集合中的元素按照你的想法排序。
* D:把TreeSet集合中的数据遍历写入到文本文件。
*/
public class StudentTest {
public static void main(String[] args) throws IOException {
  // 创建TreeSet集合
  TreeSet<Student> ts = new TreeSet<Student>();
  // 键盘录入三个学生
  for (int x = 1; x <= 3; x++) {
   // 键盘录入
   Scanner sc = new Scanner(System.in);
   System.out.println("请输入第" + x + "个学生的姓名:");
   String name = sc.nextLine();
   System.out.println("请输入第" + x + "个学生的语文成绩:");
   int chinese = sc.nextInt();
   System.out.println("请输入第" + x + "个学生的数学成绩:");
   int math = sc.nextInt();
   System.out.println("请输入第" + x + "个学生的英语成绩:");
   int english = sc.nextInt();
   // // 创建学生对象
   // Student s = new Student();
   // // 封装数据
   // s.setName(name);
   // s.setChinese(chinese);
   // s.setMath(math);
   // s.setEnglish(english);
   Student s = new Student(name, chinese, math, english);
   // 添加学生对象
   ts.add(s);
  }
  BufferedWriter bw = new BufferedWriter(new FileWriter("student.txt"));
  for (Student s : ts) {
   // System.out.println(s.getName() + "***" + s.getChinese() + "***"
   // + s.getMath() + "***" + s.getEnglish());
   bw.write(s.getName() + "-" + s.getChinese() + "-" + s.getMath()
     + "-" + s.getEnglish());
   bw.newLine();
   bw.flush();
  }
  bw.close();
}
}
在该代码中如果在录入过程中,本人不知道要录入多少个学生,且录入后以该学生的总成绩排名,应该怎么改进该代码,急求!

评分

参与人数 1技术分 +1 收起 理由
古银平 + 1 神马都是浮云

查看全部评分

9 个回复

倒序浏览
  1. //
  2. import java.io.BufferedWriter;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.util.Scanner;
  6. import java.util.TreeSet;
  7. /*
  8. * 有三个学生,每个学生有3门课的成绩,从键盘输入以上数据(姓名,三门课成绩),
  9. * 按照总成绩的排序,最后把排序后的数据写入文件。
  10. *
  11. * 思路:
  12. * A:定义学生类
  13. * B:录入3个学生,把学生存储到TreeSet集合。
  14. * C:对TreeSet集合中的元素按照你的想法排序。
  15. * D:把TreeSet集合中的数据遍历写入到文本文件。
  16. */
  17. public class StudentTest {
  18. public static void main(String[] args) throws IOException {
  19.   // 创建TreeSet集合
  20.   TreeSet<Student> ts = new TreeSet<Student>();
  21.   // 键盘录入三个学生
  22.   int x=1;//定义学生初始个数
  23. while(true) {
  24.    // 键盘录入
  25.    System.out.println("如果结束录入请输入over,否则请按回车开始录入。");
  26.    Scanner sc = new Scanner(System.in);
  27.    if("over".equals(sc.nextLine()))
  28.            break;
  29.    System.out.println("请输入第" + x + "个学生的姓名:");
  30.    String name = sc.nextLine();
  31.    System.out.println("请输入第" + x + "个学生的语文成绩:");
  32.    int chinese = sc.nextInt();
  33.    System.out.println("请输入第" + x + "个学生的数学成绩:");
  34.    int math = sc.nextInt();
  35.    System.out.println("请输入第" + x + "个学生的英语成绩:");
  36.    x++;
  37.    int english = sc.nextInt();
  38.    // // 创建学生对象
  39.     Student s = new Student();
  40.    // // 封装数据
  41.     s.setName(name);
  42.     s.setChinese(chinese);
  43.     s.setMath(math);
  44.     s.setEnglish(english);
  45.    //Student s = new Student(name, chinese, math, english);
  46.    // 添加学生对象
  47.    ts.add(s);
  48.   }
  49.   BufferedWriter bw = new BufferedWriter(new FileWriter("student.txt"));
  50.   for (Student s : ts) {
  51.    // System.out.println(s.getName() + "***" + s.getChinese() + "***"
  52.    // + s.getMath() + "***" + s.getEnglish());
  53.    bw.write(s.getName() + "-" + s.getChinese() + "-" + s.getMath()
  54.      + "-" + s.getEnglish());
  55.    bw.newLine();
  56.    bw.flush();
  57.   }
  58.   bw.close();
  59.   for(Student s:ts)
  60.         {
  61.          System.out.print(s.getName());
  62.         }

  63. }
  64. }
  65. class Student implements Comparable
  66. {

  67.         private String name;
  68.         private int chinese;
  69.         private int math;
  70.         private int english;
  71.         Student()
  72.         {
  73.         }
  74.         Student(String name,int chinese,int math,int english)
  75.         {
  76.                 this.name=name;
  77.                 this.math=math;
  78.                 this.english=english;
  79.         }
  80.         public String getName()
  81.         {
  82.                 return name;
  83.         }
  84.         public int  getChinese()
  85.         {
  86.                 return chinese;
  87.         }
  88.         public int getMath()
  89.         {
  90.                 return math;
  91.         }
  92.         public int getEnglish()
  93.         {
  94.                 return english;
  95.         }
  96.         public void setChinese(int chinese)
  97.         {
  98.                 this.chinese=chinese;
  99.         }
  100.         public void setMath(int math)
  101.         {
  102.                 this.math=math;
  103.         }
  104.         public void setEnglish(int english)
  105.         {
  106.                 this.english=english;
  107.         }
  108.         public void setName(String name)
  109.         {
  110.                 this.name=name;
  111.         }
  112.         public int compareTo(Object obj)
  113.         {
  114.                 if(obj.getClass()==Student.class)
  115.                 {
  116.                         Student stu=(Student)obj;
  117.                         return this.getChinese()+this.getMath()+this.getEnglish()-stu.getChinese()-stu.getMath()-stu.getEnglish();
  118.                 }
  119.                 else
  120.                         return 0;
  121.         }
  122.        
  123.        
  124. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
古银平 + 1 神马都是浮云

查看全部评分

回复 使用道具 举报
我的网速太慢了。楼主也没有把Student类给添加上去,导致自己还得重新写Student类。set和get方法都是用Editplus写的。我想把运行截图给你,而是网速不允许(我在教室用的是中国移动的无线网,太慢了。。。)
回复 使用道具 举报
不上运行截图。希望版主不要误认为灌水,网速确实太慢了。

StudentTest.jpg (95.18 KB, 下载次数: 34)

StudentTest.jpg

评分

参与人数 1黑马币 +3 收起 理由
朝花夕拾 + 3 很给力!

查看全部评分

回复 使用道具 举报
王震阳 发表于 2012-12-2 20:28
不上运行截图。希望版主不要误认为灌水,网速确实太慢了。

谢谢,这位兄弟,我能问一下如果我输入int型的数据的存入数组中,能实现获取排序么?假如我也不知道我要输入多少个元素?
回复 使用道具 举报
张雄 发表于 2012-12-2 22:50
谢谢,这位兄弟,我能问一下如果我输入int型的数据的存入数组中,能实现获取排序么?假如我也不知道我要 ...

单纯数组是靠脚标排序的,你可以把你的意思再表达清楚一点吗?
回复 使用道具 举报
王震阳 发表于 2012-12-2 20:28
不上运行截图。希望版主不要误认为灌水,网速确实太慢了。

你那么多金钱只给我三个呀,呵呵,你可以多给点的。

评分

参与人数 1黑马币 +9 收起 理由
朝花夕拾 + 9

查看全部评分

回复 使用道具 举报
王震阳 发表于 2012-12-2 22:54
单纯数组是靠脚标排序的,你可以把你的意思再表达清楚一点吗?

我的意思是我现在不知道我要输入多少个int型元素,要存入数组中,输入完成后,获取最大值和最小值,
回复 使用道具 举报
张雄 发表于 2012-12-3 00:19
我的意思是我现在不知道我要输入多少个int型元素,要存入数组中,输入完成后,获取最大值和最小值, ...

这个是间接可以的,随后代码给你。
回复 使用道具 举报
张雄 发表于 2012-12-3 00:19
我的意思是我现在不知道我要输入多少个int型元素,要存入数组中,输入完成后,获取最大值和最小值, ...

不建议楼主用这样的方法,很少这样子求最大最小值的其实。但是为了满足楼主要的功能还是写了一下。
  1. import java.util.*;
  2. class  getMaxMin
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 List<Integer> list=new ArrayList<Integer>();//我们必须先定义一个集合存放数据然后将集合中的数据存放在数组中
  7.                 int n=0;
  8.                 while(true)
  9.                 {
  10.                         Scanner sc = new Scanner(System.in);
  11.                         System.out.println("输入over结束,回车继续。");
  12.                         String str=sc.nextLine();
  13.                         if("over".equals(str))
  14.                                 break;
  15.                         n++;
  16.                         System.out.println("请输入第"+n+"一个整数吧");
  17.                                 list.add(sc.nextInt());
  18.                 }
  19.                 int[] num=new int[list.size()];
  20.                 n=0;
  21.                 for(Integer ing:list)
  22.                 {
  23.                         num[n++]=ing;
  24.                 }
  25.                 System.out.println("最大值:"+getMax(num));
  26.                 System.out.println("最小值:"+getMin(num));
  27.         }
  28.         public static int getMax(int[] num)
  29.         {
  30.                 int max=0;
  31.                 if(num.length>0)
  32.                          max=num[0];
  33.                 for(int tem:num)
  34.                 {
  35.                         if(max<tem)
  36.                                 max=tem;
  37.                 }
  38.                 return max;
  39.         }
  40.         public static int getMin(int[] num)
  41.         {
  42.                 int min=0;
  43.                 if(num.length>0)
  44.                          min=num[0];
  45.                 for(int tem:num)
  46.                 {
  47.                         if(min>tem)
  48.                                 min=tem;
  49.                 }
  50.                 return min;
  51.         }
  52. }
复制代码

getMaxMin其实求最大最小这种方法很不好.jpg (56.34 KB, 下载次数: 30)

getMaxMin其实求最大最小这种方法很不好.jpg
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马