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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 洋葱头头   /  2016-1-31 15:37  /  16317 人查看  /  168 人回复  /   3 人收藏 转载请遵从CC协议 禁止商业使用本文


谢自己干什么
回复 使用道具 举报
楼主,快快打赏。我没只学到13天,IO流什么的都是临时看的几集,可能用得不好。不过功能是算是实现了的。



Test.zip (2.16 KB, 下载次数: 109)



/*
写这个程序的时候我们都还没学IO流,所以也是临时看了几个视频写的,途中修修改改了好多次。也算是第一个用到IO流的程序吧,或许写得不怎么好,不过这只是个开始,今后会越来越好的。


1、先创建两个数组str和score用于存放输入的数据,创建的长度为6是因为作者本人比较懒,最后一个长度用来作为TEMP
2、使用for语句录入学生考试信息记录到str数组中,并计算总分记录到score数组里
3、使用for循环嵌套,判断总分大小并交换两个数组的内容
4、将str数组遍历输出到stu.txt文件中
*/

import java.util.Scanner;
import java.io.*;
class Test {
        public static void main(String[] args) throws IOException {
                String[] str = new String[6];        //创建用来存储录入数据的数组,str[5]用来后面交换数组中数据用的临时存储
                int[] score = new int[6];        //创建用来存储总分的数组,同上
                System.out.println("请输入(姓名,学号,语文,数学,英文)的格式进行录入数据!");        //提示用户输入的数据格式
                Scanner sc = new Scanner(System.in);        //创建一个类
                for (int i = 0; i < 5; i++) {        //for循环,从下标0开始,到下标为4结束
                        System.out.println("请输入第" + (i + 1) + "名学生的考试信息:");
                        str = sc.nextLine();        //输入学生考试的信息
                        String[] arrstr = str.split(",");        //分割字符串并将结果赋值给数组arrstr
                        //计算总分
                        score += Integer.parseInt(arrstr[2]);       
                        score += Integer.parseInt(arrstr[3]);
                        score += Integer.parseInt(arrstr[4]);
                        System.out.println(score);        //打印总分,可以用来直观检验计算结果是否正确
                }
               
                //两层for循环嵌套,类似九九乘法表,就不解释了
                for (int i = 0; i < 4; i++) {
                        for (int j = i + 1; j < 5; j++) {
                                //如果下一个索引中的总分大于上一个索引的总分,则交换其中的数据
                                if (score < score[j]) {
                                        score[5] = score;
                                        score = score[j];
                                        score[j] = score[5];
                                        str[5] = str;
                                        str = str[j];
                                        str[j] = str[5];
                                }
                        }
                }

                System.out.println("");        //仅为了美观,换行
               
                FileWriter fw = new FileWriter("stu.txt");        //打开STU.TXT文件
               
                for (int i = 0; i < 5; i++) {        //for循环,将数据写入到stu.txt中
                        fw.write(str);
                        System.out.println(str);
                        fw.write("\r\n");
                }
               
                fw.close();        //结束操作,关闭IO流
                System.out.println("");
                System.out.println("已经将结果写入stu.txt文件中。");        //提示结束程序
        }
}

评分

参与人数 1技术分 +4 收起 理由
洋葱头头 + 4

查看全部评分

回复 使用道具 举报
wusiyi 发表于 2016-2-13 21:01
楼主,快快打赏。我没只学到13天,IO流什么的都是临时看的几集,可能用得不好。不过功能是算是实现了的。

...

先给分  明天再看 睡了
回复 使用道具 举报
{:2_32:}以后有送分题就CALL ME。
回复 使用道具 举报
洋葱头头 发表于 2016-2-13 22:54
先给分  明天再看 睡了

Test.zip文件直接该后缀名为.java就可以了,那个不是真的压缩包。
回复 使用道具 举报
这么好的事居然才看见!
回复 使用道具 举报
wusiyi 发表于 2016-2-13 23:00
以后有送分题就CALL ME。

今天就有
回复 使用道具 举报
本帖最后由 洋葱头头 于 2016-2-14 20:38 编辑


  1. import java.io.*;
  2. import java.util.*;
  3. public class Test {
  4.     public static void main(String[] args)throws Exception{
  5.         //传入学生人数
  6.         Student(5);
  7.     }
  8.     //键盘录入学生
  9.     public static void Student(int number)throws Exception{
  10.         //创建Scanner对象 接受从控制台输入
  11.         Scanner in=new Scanner(System.in);
  12.         //因为可能会出现姓名和总分都一样的学生,为了保证学生不丢失,建立List容器
  13.         List list=new ArrayList();
  14.         //循环录入学生到集合,排好顺序
  15.         System.out.println("输入格式为: 姓名,语文,数学,英语");
  16.         for(int x=1;x<=number;x++){
  17.             System.out.println("请输入第"+x+"个学生的信息");
  18.             String[] s=in.nextLine().split(",");
  19.             int a=Integer.parseInt(s[1]);
  20.             int b=Integer.parseInt(s[2]);
  21.             int c=Integer.parseInt(s[3]);
  22.             Student stu=new Student(s[0],a,b,c);
  23.             list.add(stu);
  24.         }
  25.         //用比较器进行总分从高到低的排序
  26.         Collections.sort(list,new StuCompare());
  27.         //定义输出流输出到stu.txt
  28.         BufferedWriter bufw=new BufferedWriter(new FileWriter("stu.txt"));
  29.         for(Student stu:list){
  30.             bufw.write(stu.getStu());
  31.             bufw.newLine();
  32.             bufw.flush();
  33.         }
  34.         //关闭资源
  35.         in.close();
  36.         bufw.close();
  37.     }
  38. }
  39. //学生类型
  40. class Student{
  41.     private String name;
  42.     private int a, b, c,sum;
  43.     //学生对象建立需要传入姓名和3门课的成绩
  44.     Student(String name, int a, int b, int c){
  45.         this.name=name;
  46.         this.a=a;
  47.         this.b=b;
  48.         this.c=c;
  49.         sum=a+b+c;
  50.     }
  51.     public int getsum(){
  52.         return sum;
  53.     }
  54.     //输出一个符合格式的字符串
  55.     public String getStu(){
  56.         return name+"    数学:"+a+"    语文:"+b+"    英语:"+c+"    总分:"+sum;
  57.     }
  58. }
  59. //定义比较器,按总分排序
  60. class StuCompare implements Comparator{
  61.     public int compare(Student s1,Student s2){
  62.         int a=new Integer(s2.getsum()).compareTo(s1.getsum());
  63.         return a;
  64.     }
复制代码


评分

参与人数 1技术分 +3 收起 理由
洋葱头头 + 3

查看全部评分

回复 使用道具 举报
zhang洁 来自手机 中级黑马 2016-2-14 20:16:15
89#
这么好?才看到
回复 使用道具 举报
刺客015 发表于 2016-2-14 20:01
/*
没压缩成功不好意思
*/

.你这排版
回复 使用道具 举报

哪里啊,我怎么没有看到呢、
回复 使用道具 举报
wusiyi 发表于 2016-2-14 21:56
哪里啊,我怎么没有看到呢、

明天早上发... 困死了
回复 使用道具 举报
回复 使用道具 举报
回复 使用道具 举报
现在做这道题,还有技术分吗
回复 使用道具 举报
参禅悟道 发表于 2016-2-15 16:22
现在做这道题,还有技术分吗

有的 这题比较比较好写
回复 使用道具 举报
用的冒泡排序,最后把打印的功能提到一个方法里面了。

studentFen.rar

1.3 KB, 阅读权限: 100, 下载次数: 1

版主查阅

点评

收到 已给分  发表于 2016-2-15 17:51

评分

参与人数 1技术分 +3 收起 理由
洋葱头头 + 3 赞一个!

查看全部评分

回复 使用道具 举报
还没学到..
回复 使用道具 举报

加紧学习了
回复 使用道具 举报
求分分,绝对没问题,我的路径写的E盘哈

学生成绩排名哦.zip

790 Bytes, 下载次数: 69

评分

参与人数 1技术分 +3 收起 理由
洋葱头头 + 3

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马