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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 吴海松 中级黑马   /  2014-12-25 20:56  /  689 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

d盘有个文件c.txt内容如下:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
要求:将c.txt中的数字进行如下运算,第n行m列是第(n-1)行第m列的数字之和(提示:第一行不变,第二行变为:6 8 10 12,第三行变为:15 18 21 24,第四行变为28 32 36 40),然后将计算结保存在d.txt中。
有好的建议和意见欢迎提出交流,最好可以帮我改进一下代码
  1. package heima;

  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.io.PrintWriter;
  8. import java.util.ArrayList;
  9. import java.util.regex.Matcher;
  10. import java.util.regex.Pattern;

  11. public class Test1 {
  12.         static Pattern p = null;
  13.         static Matcher mathcer = null;
  14.         static String regex = "\\b[0-9]++\\b";
  15.         static ArrayList<ArrayList> arr = new ArrayList<ArrayList>();// 每行的号码都用一个集合存起来,这个集合用来存下面的集合存号码的集合

  16.         public static void main(String[] args) throws IOException {
  17.                 show("D:\\d.txt");

  18.         }

  19.         public static void show(String FileDir) throws IOException {
  20.                 File f = new File(FileDir);
  21.                 BufferedReader r = new BufferedReader(new FileReader(f));
  22.                 String str = null;
  23.                 int line = 1;// 行计数器,用来记录读到了第几行
  24.                 int com = 0;// 用于重复记数,0只是用来初始化而已
  25.                 while ((str = r.readLine()) != null) {
  26.                         p = Pattern.compile(regex);// 这两行是正则匹配
  27.                         mathcer = p.matcher(str);
  28.                         ArrayList arr1 = new ArrayList<Integer>(); // 用于存储每行的号码,由于在循环内,所以每次执行到这里都会创建一个新的集合,
  29.                         if (line == 1) {// 这个是获取第一行的数据,第二行开始就不需要执行这个if内的语句了,因为第二行开始只受第一行的最后一个号码影响结果而已
  30.                                 while (mathcer.find()) {
  31.                                         Integer x = Integer.parseInt(mathcer.group());
  32.                                         arr1.add(x);
  33.                                         com = x;// 号码增加集合之后,我要记录下来这个号码,用来运算的,下一行开始到最后执行的都是else语句了
  34.                                 }

  35.                         } else {
  36.                                 while (mathcer.find()) {// 从第二行开始都会执行到这里,直到最后
  37.                                                                                 // ;mathcer.find()是匹配器的方法
  38.                                         com = com + line;// 第二行之后,其实读到的数字是什么并不重要,后一个数是前一个数加上行数line即可
  39.                                         arr1.add(com);
  40.                                 }
  41.                         }
  42.                         arr.add(arr1);// 将装行号码的集合增加到在成员变量定义的那个集合去
  43.                         line++;// 是行计数器累加
  44.                 }
  45.                 PrintWriter out = new PrintWriter(new FileWriter(new File("d:\\c.txt")),true);

  46.                 for (ArrayList ar : arr) {
  47.                         for (int i = 0; i < ar.size(); i++) {
  48.                                         out.write(ar.get(i).toString()+" ");
  49.                                         out.flush();

  50.                         }
  51.                         out.println("");
  52.                 }

  53.                
  54.                  for (ArrayList ar : arr) { for (int i = 0; i < ar.size(); i++) {
  55.                   System.out.print((int) ar.get(i) + " ");
  56.                   
  57.                   } System.out.println(" "); }
  58.                  

  59.         }

  60. }
复制代码

1 个回复

倒序浏览
没看楼主的代码,都差不多吧。
  1. public class Test1
  2. {
  3.         public static void main(String[] args) throws Exception
  4.         {
  5.                 File res=new File("d:\\a.txt");
  6.                 File des=new File("d:\\b.txt");
  7.                 BufferedReader br=new BufferedReader(new FileReader(res));
  8.                 BufferedWriter bw=new BufferedWriter(new FileWriter(des));
  9.                 int num=0;
  10.                 String line=null;
  11.                 while((line=br.readLine())!=null)
  12.                 {
  13.                         num++;
  14.                         String[] arr=line.split(" ");
  15.                         if(num%2==0)
  16.                         {
  17.                                 arr=sort(arr);
  18.                                 for(String str:arr)
  19.                                         bw.write(str+" ");
  20.                                 bw.newLine();
  21.                         }
  22.                         else
  23.                         {
  24.                                 arr=swap(arr);
  25.                                 for(String str:arr)
  26.                                         bw.write(str+" ");
  27.                                 bw.newLine();
  28.                         }
  29.                 }
  30.                 br.close();
  31.                 bw.close();
  32.         }
  33.         private static String[] sort(String[] arr)
  34.         {
  35.                 for(int i=0;i<arr.length/2;i++)
  36.                 {
  37.                         String temp=arr[i];
  38.                         arr[i]=arr[arr.length-1-i];
  39.                         arr[arr.length-1-i]=temp;
  40.                 }
  41.                 return arr;
  42.         }
  43.         private static String[] swap(String[] arr)
  44.         {
  45.                 for(int i=0;i<arr.length;i++)
  46.                 {
  47.                         String str=arr[i].charAt(1)+""+arr[i].charAt(0);
  48.                         arr[i]=str;
  49.                 }
  50.                 return arr;
  51.         }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马