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

asdf344180788

  • 黑马币:12

  • 帖子:5

  • 精华:0

© asdf344180788   /  2015-11-10 23:48  /  8456 人查看  /  29 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

Weidan 中级黑马 2015-11-28 21:01:26
21#
给你个看起来简单点的,打印出来的效果是这样的:
望采纳啊亲

  1. package cn.liweidan.test1;

  2. import java.io.BufferedOutputStream;
  3. import java.io.File;
  4. import java.io.FileDescriptor;
  5. import java.io.FileOutputStream;
  6. import java.io.PrintStream;
  7. import java.util.ArrayList;
  8. import java.util.Collections;
  9. import java.util.Comparator;
  10. import java.util.List;

  11. /**
  12. * 有五个学生,每个学生有3门课(语文、数学、英语)的成绩,写一个程序接收从键盘输入学生的信息,
  13. * 输入格式为:name,30,30,30(姓名,三门课成绩),然后把输入的学生信息按总分从高到低的顺序写入到一个
  14. * 名称"stu.txt"文件中。要求:stu.txt文件的格式要比较直观,打开这个文件,就可以很清楚的看到学生的信息。
  15. *
  16. * @author Weidan
  17. *
  18. */
  19. public class Demo02 {

  20.         public static void main(String[] args) throws Exception {
  21.                 // 重定向
  22.                 System.setOut(new PrintStream(new BufferedOutputStream(
  23.                                 new FileOutputStream(new File("stu.txt"))), true)); // 设置输出到文件,true表示输出后自动刷新
  24.                 System.out.println("姓名\t语文\t数学\t英语\t总分");
  25.                 // 创建容器
  26.                 List<Student> list = new ArrayList<Student>();
  27.                 // 创建对象
  28.                 Student s1 = new Student("张三", 90, 80, 75);
  29.                 Student s2 = new Student("李四", 95, 100, 75);
  30.                 Student s3 = new Student("王五", 70, 85, 89);
  31.                 // 分别添加到容器当中去
  32.                 list.add(s1);
  33.                 list.add(s2);
  34.                 list.add(s3);
  35.                 // 进行排序
  36.                 Collections.sort(list, new score());
  37.                 // 遍历输出学生信息
  38.                 for (Student student : list) {
  39.                         System.out.println(student.toString());
  40.                 }
  41.                 // 把输出还原为控制台
  42.                 System.setOut(new PrintStream(new BufferedOutputStream(
  43.                                 new FileOutputStream(FileDescriptor.out)), true));// 设置回到控制台
  44.                 System.out.println("程序运行结束");

  45.         }

  46. }

  47. class Student {
  48.         private String name;
  49.         private int chineseSco;
  50.         private int mathSco;
  51.         private int englishSco;

  52.         public Student() {

  53.         }

  54.         public Student(String name, int chineseSco, int mathSco, int englishSco) {
  55.                 super();
  56.                 this.name = name;
  57.                 this.chineseSco = chineseSco;
  58.                 this.mathSco = mathSco;
  59.                 this.englishSco = englishSco;
  60.         }

  61.         public String getName() {
  62.                 return name;
  63.         }

  64.         public void setName(String name) {
  65.                 this.name = name;
  66.         }

  67.         public int getChineseSco() {
  68.                 return chineseSco;
  69.         }

  70.         public void setChineseSco(int chineseSco) {
  71.                 this.chineseSco = chineseSco;
  72.         }

  73.         public int getMathSco() {
  74.                 return mathSco;
  75.         }

  76.         public void setMathSco(int mathSco) {
  77.                 this.mathSco = mathSco;
  78.         }

  79.         public int getEnglishSco() {
  80.                 return englishSco;
  81.         }

  82.         public void setEnglishSco(int englishSco) {
  83.                 this.englishSco = englishSco;
  84.         }

  85.         //获得总分
  86.         public int getScore() {
  87.                 return chineseSco + mathSco + englishSco;
  88.         }

  89.         @Override
  90.         public String toString() {
  91.                 return name + "\t" + chineseSco + "\t" + mathSco + "\t" + englishSco
  92.                                 + "\t" + getScore();
  93.         }

  94. }

  95. /**
  96. * 业务排序类
  97. * @author Weidan
  98. *
  99. */
  100. class score implements Comparator<Student> {

  101.         /**
  102.          * 如果s1的总分比s2少,返回1,表示s2排在前面
  103.          */
  104.         @Override
  105.         public int compare(Student s1, Student s2) {
  106.                 if (s1.getScore() - s2.getScore() < 0) {
  107.                         return 1;
  108.                 } else if (s1.getScore() - s2.getScore() == 0) {
  109.                         return 0;
  110.                 } else {
  111.                         return -1;
  112.                 }
  113.         }
  114. }
复制代码
回复 使用道具 举报
import java.io.*;
import java.util.*;
public class lianxi50 {
public static void main(String[] args){
   Scanner ss = new Scanner(System.in);
   String [][] a = new String[5][6];
   for(int i=1; i<6; i++) {
    System.out.print("请输入第"+i+"个学生的学号:");
    a[i-1][0] = ss.nextLine();
    System.out.print("请输入第"+i+"个学生的姓名:");
    a[i-1][1] = ss.nextLine();
    for(int j=1; j<4; j++) {
       System.out.print("请输入该学生的第"+j+"个成绩:");
       a[i-1][j+1] = ss.nextLine();
       }
System.out.println("\n");
   }
//以下计算平均分
float avg;
int sum;
for(int i=0; i<5; i++) {
sum=0;
   for(int j=2; j<5; j++) {
   sum=sum+ Integer.parseInt(a[i][j]);
      }
   avg= (float)sum/3;
   a[i][5]=String.valueOf(avg);
}
//以下写磁盘文件
String s1;
try {
    File f = new File("C:\\stud");
    if(f.exists()){
      System.out.println("文件存在");
      }else{
         System.out.println("文件不存在,正在创建文件");
          f.createNewFile();//不存在则创建
        }
BufferedWriter output = new BufferedWriter(new FileWriter(f));
for(int i=0; i<5; i++) {
for(int j=0; j<6; j++) {
   s1=a[i][j]+"\r\n";
   output.write(s1);   
    }
}
output.close();
System.out.println("数据已写入c盘文件stud中!");
   } catch (Exception e) {
     e.printStackTrace();
     }
}
}





import java.util.Arrays;
import java.util.Random;

/*
回复 使用道具 举报
  1. package exp;

  2. import java.io.File;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.math.BigDecimal;
  6. import java.util.ArrayList;
  7. import java.util.Scanner;

  8. /**
  9. *
  10. * 有五个学生,每个学生有3门课(语文、数学、英语)的成绩,写一个程序接收从键盘输入学生的信息,
  11. * 输入格式为:name,30,30,30(姓名,三门课成绩),然后把输入的学生信息按总分从高到低的顺序写入到一个
  12. * 名称"stu.txt"文件中。要求:stu.txt文件的格式要比较直观,打开这个文件,就可以很清楚的看到学生的信息。
  13. *
  14. * @author VisionDo
  15. *
  16. */
  17. public class StudentScoreInf {

  18.         private static final int MAX = 5;//学生总数
  19.         private static final String FilePath = "stu.txt";//文件名
  20.        
  21.         private static ArrayList<Student> StuArray;//保存学生对象的数组
  22.        
  23.        
  24.        
  25.         public StudentScoreInf(){
  26.                 StuArray = new ArrayList<Student>();
  27.         }
  28.        
  29.        
  30.         public static void main(String[] args) {
  31.                
  32.                 StudentScoreInf ssi = new StudentScoreInf();
  33.                 ssi.InitialStudentInf();
  34.                 try {
  35.                         SortStu(StuArray);//对录入的学生信息按总分排序
  36.                         WriteStuInf(StuArray);//将学生信息写入文件
  37.                 } catch (IOException e) {
  38.                         System.out.println("文件写入失败");
  39.                 }
  40.                
  41.         }
  42.        
  43.         //初始化学生信息
  44.         private void InitialStudentInf(){
  45.                
  46.                 System.out.println("请输入学生的信息:");
  47.                 System.out.println("--------------------------------------");
  48.                 System.out.println("输入格式为:name,30,30,30(语文、数学、英语)");
  49.                 System.out.println("--------------------------------------");
  50.                
  51.                 Scanner sc = new Scanner(System.in);
  52.                 String inf = "";
  53.             int flag = 0;
  54.                 while(flag < MAX ){
  55.                         inf = sc.next();
  56.                         if(IsRight(inf)){
  57.                                 flag++;
  58.                         }else
  59.                                 System.out.println("数据有误,请重新输入:");       
  60.                 }
  61.                
  62.         }
  63.        
  64.         //将用户录入的学生信息,写入指定文件内
  65.         private static void WriteStuInf(ArrayList<Student> inf) throws IOException{
  66.                
  67.                 File file = new File(FilePath);

  68.                 if(!file.exists())
  69.                         file.createNewFile();//如果文件不存在,创建新文件
  70.                 else{
  71.                         FileWriter fw = new FileWriter(file,false);
  72.                         fw.write("");//如果文件已经存在,清空文件
  73.                         fw.close();
  74.                 }
  75.                
  76.                 FileWriter fw = new FileWriter(file,true);//以追加的方式写入
  77.                
  78.                 StringBuffer title = new StringBuffer();
  79.                
  80.                 //制作表头
  81.                 title.append("\t\t学生成绩表");
  82.                 title.append("\r\n------------------------------------------\r\n");
  83.                 title.append("姓名\t语文\t数学\t英语\t总分\t\r\n");
  84.                 fw.write(title.toString());
  85.                 title.delete(0, title.length());//清空
  86.                 //写入信息
  87.                 for(Student s:inf){
  88.                         title.append("\r\n");
  89.                         title.append(s.getName()+"\t");
  90.                         title.append(s.getChinese()+"\t");
  91.                         title.append(s.getMath()+"\t");
  92.                         title.append(s.getEnglish()+"\t");
  93.                         title.append(s.getTotalScore()+"\t\t");
  94.                         title.append("\r\n");
  95.                         fw.write(title.toString());
  96.                         title.delete(0, title.length());//清空
  97.                 }
  98.                
  99.                 fw.close();
  100.                 System.out.println("学生成绩登记完毕!");               
  101.         }
  102.        
  103.        
  104.         /**
  105.          *
  106.          * @param str 用户输入的学生信息
  107.          * @return  录入的信息是否正确
  108.          */
  109.         private boolean IsRight(String str){
  110.                
  111.                 boolean flag =str.trim().isEmpty()?false:true;//判断是否为空
  112.                 Object[] StuInf = str.split(",");
  113.                 flag = StuInf.length<4?false:true;//判断数据是否齐全
  114.                
  115.                 if(flag){
  116.                         int i =1;
  117.                         double score = 0.0;
  118.                         while(i<StuInf.length){
  119.                                 score = Double.valueOf((String) StuInf[i]);//得到单科成绩
  120.                                 if(score<0 || score >100)
  121.                                         return false;
  122.                                 else
  123.                                         i++;
  124.                         }
  125.                         //while循环执行完成后,如果当前循环变量i的值大于4
  126.                    //学生的单条信息,按逗号切割后,数组大小为4
  127.                         if(i >= StuInf.length){
  128.                                 flag = true;
  129.                        
  130.                                 score = 0.0;//计算总分
  131.                                 for(int j =1;j<StuInf.length;j++){
  132.                                         score += Double.valueOf((String)StuInf[j]);
  133.                                 }
  134.                                
  135.                                 String name = (String)StuInf[0];//姓名
  136.                                 double chinese = Double.valueOf((String)StuInf[1]);//语文成绩
  137.                                 double math = Double.valueOf((String)StuInf[2]);//数学成绩
  138.                                 double english = Double.valueOf((String)StuInf[3]);//英语成绩
  139.                                
  140.                                 //对总分保留两位小数
  141.                                 BigDecimal bd = new BigDecimal(score);
  142.                                 score = bd.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
  143.                                
  144.                                 //实例化对应的学生
  145.                                 Student stu = new Student(name,chinese,math,english,score);
  146.                                 StuArray.add(stu);
  147.                                 return true;
  148.                         }
  149.                                
  150.                         else
  151.                                 flag = false;
  152.                        
  153.                         return flag;
  154.                 }else
  155.                         return flag;
  156.                
  157.         }
  158.        
  159.         /*
  160.          * 将学生按总分由高到低排序
  161.          *
  162.          */
  163.         private static void SortStu(ArrayList<Student> stu){
  164.                 Student temp;
  165.                 for(int i = 0; i< stu.size()-1; i++){
  166.                         for(int j =i+ 1; j<stu.size();j++){
  167.                                 if(stu.get(i).getTotalScore() < stu.get(j).getTotalScore()){
  168.                                         temp = stu.get(j);
  169.                                         stu.set(j,stu.get(i));
  170.                                         stu.set(i, temp);
  171.                                 }
  172.                                
  173.                         }
  174.                 }
  175.         }
  176.        
  177.        
  178.         //学生对象
  179.         class Student{
  180.                 String name;
  181.                 double chinese;
  182.                 double math;
  183.                 double english;
  184.                 double TotalScore = 0.0;
  185.                
  186.                 public Student(){}
  187.                
  188.                 public Student(String name,double chinese,double math,double english,double TotalScore){
  189.                         this.name = name;
  190.                         this.chinese = chinese;
  191.                         this.math = math;
  192.                         this.english = english;
  193.                         this.TotalScore = TotalScore;
  194.                 }
  195.                
  196.                
  197.                 public String getName() {
  198.                         return name;
  199.                 }

  200.                 public void setName(String name) {
  201.                         this.name = name;
  202.                 }

  203.                 public double getChinese() {
  204.                         return chinese;
  205.                 }

  206.                 public void setChinese(double chinese) {
  207.                         this.chinese = chinese;
  208.                 }

  209.                 public double getMath() {
  210.                         return math;
  211.                 }

  212.                 public void setMath(double math) {
  213.                         this.math = math;
  214.                 }

  215.                 public double getEnglish() {
  216.                         return english;
  217.                 }

  218.                 public void setEnglish(double english) {
  219.                         this.english = english;
  220.                 }

  221.                 public double getTotalScore() {
  222.                         return TotalScore;
  223.                 }

  224.                 public void setTotalScore(double totalScore) {
  225.                         TotalScore = totalScore;
  226.                 }
  227.                
  228.                
  229.                
  230.         }
  231.        

  232. }
复制代码
回复 使用道具 举报
路过,看大牛的。。
回复 使用道具 举报
Mykey 中级黑马 2015-12-14 22:15:20
25#
这么牛逼吗,我考了,可是没这么难啊,.
回复 使用道具 举报
lixy 中级黑马 2015-12-15 22:41:48
26#
那么多大神都粘出代码,我就不献丑了。楼主好好学学啊
回复 使用道具 举报
12
您需要登录后才可以回帖 登录 | 加入黑马