- package exp;
 
  
- import java.io.File;
 
 - import java.io.FileWriter;
 
 - import java.io.IOException;
 
 - import java.math.BigDecimal;
 
 - import java.util.ArrayList;
 
 - import java.util.Scanner;
 
  
- /**
 
 -  * 
 
 -  * 有五个学生,每个学生有3门课(语文、数学、英语)的成绩,写一个程序接收从键盘输入学生的信息,
 
 -  * 输入格式为:name,30,30,30(姓名,三门课成绩),然后把输入的学生信息按总分从高到低的顺序写入到一个
 
 -  * 名称"stu.txt"文件中。要求:stu.txt文件的格式要比较直观,打开这个文件,就可以很清楚的看到学生的信息。
 
 -  * 
 
 -  * @author VisionDo
 
 -  *
 
 -  */
 
 - public class StudentScoreInf {
 
  
-         private static final int MAX = 5;//学生总数
 
 -         private static final String FilePath = "stu.txt";//文件名
 
 -         
 
 -         private static ArrayList<Student> StuArray;//保存学生对象的数组
 
 -         
 
 -         
 
 -         
 
 -         public StudentScoreInf(){
 
 -                 StuArray = new ArrayList<Student>();
 
 -         }
 
 -         
 
 -         
 
 -         public static void main(String[] args) {
 
 -                 
 
 -                 StudentScoreInf ssi = new StudentScoreInf();
 
 -                 ssi.InitialStudentInf();
 
 -                 try {
 
 -                         SortStu(StuArray);//对录入的学生信息按总分排序
 
 -                         WriteStuInf(StuArray);//将学生信息写入文件
 
 -                 } catch (IOException e) {
 
 -                         System.out.println("文件写入失败");
 
 -                 }
 
 -                 
 
 -         }
 
 -         
 
 -         //初始化学生信息
 
 -         private void InitialStudentInf(){
 
 -                 
 
 -                 System.out.println("请输入学生的信息:");
 
 -                 System.out.println("--------------------------------------");
 
 -                 System.out.println("输入格式为:name,30,30,30(语文、数学、英语)");
 
 -                 System.out.println("--------------------------------------");
 
 -                 
 
 -                 Scanner sc = new Scanner(System.in);
 
 -                 String inf = "";
 
 -             int flag = 0;
 
 -                 while(flag < MAX ){
 
 -                         inf = sc.next();
 
 -                         if(IsRight(inf)){
 
 -                                 flag++;
 
 -                         }else
 
 -                                 System.out.println("数据有误,请重新输入:");        
 
 -                 }
 
 -                 
 
 -         }
 
 -         
 
 -         //将用户录入的学生信息,写入指定文件内
 
 -         private static void WriteStuInf(ArrayList<Student> inf) throws IOException{
 
 -                 
 
 -                 File file = new File(FilePath);
 
  
-                 if(!file.exists())
 
 -                         file.createNewFile();//如果文件不存在,创建新文件
 
 -                 else{
 
 -                         FileWriter fw = new FileWriter(file,false);
 
 -                         fw.write("");//如果文件已经存在,清空文件
 
 -                         fw.close();
 
 -                 }
 
 -                 
 
 -                 FileWriter fw = new FileWriter(file,true);//以追加的方式写入
 
 -                 
 
 -                 StringBuffer title = new StringBuffer();
 
 -                 
 
 -                 //制作表头
 
 -                 title.append("\t\t学生成绩表");
 
 -                 title.append("\r\n------------------------------------------\r\n");
 
 -                 title.append("姓名\t语文\t数学\t英语\t总分\t\r\n");
 
 -                 fw.write(title.toString());
 
 -                 title.delete(0, title.length());//清空
 
 -                 //写入信息
 
 -                 for(Student s:inf){
 
 -                         title.append("\r\n");
 
 -                         title.append(s.getName()+"\t");
 
 -                         title.append(s.getChinese()+"\t");
 
 -                         title.append(s.getMath()+"\t");
 
 -                         title.append(s.getEnglish()+"\t");
 
 -                         title.append(s.getTotalScore()+"\t\t");
 
 -                         title.append("\r\n");
 
 -                         fw.write(title.toString());
 
 -                         title.delete(0, title.length());//清空
 
 -                 }
 
 -                 
 
 -                 fw.close();
 
 -                 System.out.println("学生成绩登记完毕!");                
 
 -         }
 
 -         
 
 -         
 
 -         /**
 
 -          * 
 
 -          * @param str 用户输入的学生信息
 
 -          * @return  录入的信息是否正确
 
 -          */
 
 -         private boolean IsRight(String str){
 
 -                 
 
 -                 boolean flag =str.trim().isEmpty()?false:true;//判断是否为空
 
 -                 Object[] StuInf = str.split(",");
 
 -                 flag = StuInf.length<4?false:true;//判断数据是否齐全
 
 -                 
 
 -                 if(flag){
 
 -                         int i =1;
 
 -                         double score = 0.0; 
 
 -                         while(i<StuInf.length){
 
 -                                 score = Double.valueOf((String) StuInf[i]);//得到单科成绩
 
 -                                 if(score<0 || score >100)
 
 -                                         return false;
 
 -                                 else
 
 -                                         i++;
 
 -                         }
 
 -                         //while循环执行完成后,如果当前循环变量i的值大于4
 
 -                    //学生的单条信息,按逗号切割后,数组大小为4
 
 -                         if(i >= StuInf.length){
 
 -                                 flag = true;
 
 -                         
 
 -                                 score = 0.0;//计算总分
 
 -                                 for(int j =1;j<StuInf.length;j++){
 
 -                                         score += Double.valueOf((String)StuInf[j]);
 
 -                                 }
 
 -                                 
 
 -                                 String name = (String)StuInf[0];//姓名
 
 -                                 double chinese = Double.valueOf((String)StuInf[1]);//语文成绩
 
 -                                 double math = Double.valueOf((String)StuInf[2]);//数学成绩
 
 -                                 double english = Double.valueOf((String)StuInf[3]);//英语成绩
 
 -                                 
 
 -                                 //对总分保留两位小数
 
 -                                 BigDecimal bd = new BigDecimal(score);
 
 -                                 score = bd.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
 
 -                                 
 
 -                                 //实例化对应的学生
 
 -                                 Student stu = new Student(name,chinese,math,english,score);
 
 -                                 StuArray.add(stu);
 
 -                                 return true;
 
 -                         }
 
 -                                 
 
 -                         else
 
 -                                 flag = false;
 
 -                         
 
 -                         return flag;
 
 -                 }else
 
 -                         return flag;
 
 -                 
 
 -         }
 
 -         
 
 -         /*
 
 -          * 将学生按总分由高到低排序
 
 -          * 
 
 -          */
 
 -         private static void SortStu(ArrayList<Student> stu){
 
 -                 Student temp;
 
 -                 for(int i = 0; i< stu.size()-1; i++){
 
 -                         for(int j =i+ 1; j<stu.size();j++){
 
 -                                 if(stu.get(i).getTotalScore() < stu.get(j).getTotalScore()){
 
 -                                         temp = stu.get(j);
 
 -                                         stu.set(j,stu.get(i));
 
 -                                         stu.set(i, temp);
 
 -                                 }
 
 -                                 
 
 -                         }
 
 -                 }
 
 -         }
 
 -         
 
 -         
 
 -         //学生对象
 
 -         class Student{
 
 -                 String name;
 
 -                 double chinese;
 
 -                 double math;
 
 -                 double english;
 
 -                 double TotalScore = 0.0;
 
 -                 
 
 -                 public Student(){}
 
 -                 
 
 -                 public Student(String name,double chinese,double math,double english,double TotalScore){
 
 -                         this.name = name;
 
 -                         this.chinese = chinese;
 
 -                         this.math = math;
 
 -                         this.english = english;
 
 -                         this.TotalScore = TotalScore;
 
 -                 }
 
 -                 
 
 -                 
 
 -                 public String getName() {
 
 -                         return name;
 
 -                 }
 
  
-                 public void setName(String name) {
 
 -                         this.name = name;
 
 -                 }
 
  
-                 public double getChinese() {
 
 -                         return chinese;
 
 -                 }
 
  
-                 public void setChinese(double chinese) {
 
 -                         this.chinese = chinese;
 
 -                 }
 
  
-                 public double getMath() {
 
 -                         return math;
 
 -                 }
 
  
-                 public void setMath(double math) {
 
 -                         this.math = math;
 
 -                 }
 
  
-                 public double getEnglish() {
 
 -                         return english;
 
 -                 }
 
  
-                 public void setEnglish(double english) {
 
 -                         this.english = english;
 
 -                 }
 
  
-                 public double getTotalScore() {
 
 -                         return TotalScore;
 
 -                 }
 
  
-                 public void setTotalScore(double totalScore) {
 
 -                         TotalScore = totalScore;
 
 -                 }
 
 -                 
 
 -                 
 
 -                 
 
 -         }
 
 -         
 
  
- }
 
 
  复制代码 |