//现有如下数据:
//(1)计算每行的和,并输出结果;
//(2)计算所有奇数的和,并输出结果;
//(3)计算所有满足n*n的数的和,并输出结果;
//(4)计算所有素数的和,并输出结果。
//(5)将上述结果,写入文件2.text。
//注:行标志为#
//    读取文件时,仅仅分析<data>  </data>之间的数据
<data>
#  1 ,  2 ,  3 ,  4 ,  5 ,  6 ,  7 ,  8 ,  9 , 10 # 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 
# 21 , 22 , 23 , 24 , 25 , 26 , 27 , 28 , 29 , 30 , 31 , 32 , 33 , 34 , 35 , 36 , 37 , 38 , 39 , 40 
# 41 , 42 , 43 , 44 , 45 , 46 , 47 , 48 , 49 , 50 # 51 , 52 , 53 , 54 , 55 , 56 , 57 , 58 , 59 , 60 
# 61 , 62 , 63 , 64 , 65 , 66 , 67 , 68 , 69 , 70 , 71 , 72 , 73 , 74 , 75 , 76 , 77 , 78 , 79 , 80 
# 81 , 82 , 83 , 84 , 85 , 86 , 87 , 88 , 89 , 90 # 91 , 92 , 93 , 94 , 95 , 96 , 97 , 98 , 99 , 100 
</data>要求:
(1)用Java语言实现,开发环境任意(jdk或者任意IDE均可);
(2)如有可能,体现面向对象编程的一些特性,比如封装,继承,多态等;
(3)尽量遵循Java编码规范;
(4)包括一些简单的异常处理;
(5)适当的comments.(6)输出结果要求:
The sum of $NUM numbers(列出所有满足条件的数,以逗号,分隔) is: $RESULTeg: The sum of 5 numbers(1,2,3,4,5) is: 15(7)写一个批处理(bat)文件,编译运行该程序。

解决方案 »

  1.   

    1.<data>  </data>之间的数据,用正则匹配找出它们之间的数据`。"<data>(.*)</data>"
    2.拆分字符串,用split("#")
    3.拆分2中数组的每个字符串,用split(",")
    剩下的就好做了。
      

  2.   

    1: String s=null; BufferedReader in; FileReader file;
       int sum=0;
       try{ while ((s=in.readLine())!=null&&(s.startsWith("#")))
             { s=in.readLine();
               StringTokenizer fenxi=new StringTokenizer(s,",");
               int number=fenxi.countTokens();
               while(fenxi.hasMoreTokens())
               { 
                 sum=sum+Integer.parseInt(fenxi.nextToken());
               }
             }
           }
       catch(IOExcepton e){}
                 
      

  3.   

    static void writeFile(){
    try{
    FileReader r = new FileReader("C:\\my document\\hello\\1.txt");
    BufferedReader in = new BufferedReader(r);
    String s ;
    StringBuffer sb = new StringBuffer();
    while((s=in.readLine())!=null){
    sb.append(s);
    }
    in.close(); FileWriter w = new FileWriter("C:\\my document\\hello\\2.txt");
    BufferedWriter o = new BufferedWriter(w);
    String[] arrLine;
    int lineSum=0,oddSum=0,nnSum=0;

    String strData = getData(sb.toString());
    arrLine = strData.trim().substring(1).split("#");
    for(int i=0;i<arrLine.length;i++){
    String[] arrItem = arrLine[i].trim().split(",");
    lineSum = 0;
    for(int j=0;j<arrItem.length;j++){
    int item = new Integer(arrItem[j].trim());
    lineSum += item;
    oddSum += item%2==0?0:item;
    }
    o.write("The sum of " 
    + arrItem.length + " numbers("
    + arrLine[i] + ") is: " + lineSum);
    o.write("\r\n");
    System.out.println("The sum of " 
    + arrItem.length + " numbers("
    + arrLine[i] + ") is: " + lineSum);
    }
    o.write(""+oddSum);
    System.out.println("" + oddSum);

    o.close();

    }catch(Exception e){System.out.println(e.toString());}
    }
    static String getData(String str){
    Pattern p = Pattern.compile("<data>(.*)</data>");
    Matcher m = p.matcher(str);
    if(m.find()) return m.group(1);
    return "";
    }
      

  4.   

    shan1119(大天使) ( ) 信誉:101    Blog 还真有空
      

  5.   

    static void writeFile(){
    try{
    FileReader r = new FileReader("C:\\my document\\hello\\1.txt");
    BufferedReader in = new BufferedReader(r);
    String s ;
    StringBuffer sb = new StringBuffer();
    while((s=in.readLine())!=null){
    sb.append(s);
    }
    in.close(); FileWriter w = new FileWriter("C:\\my document\\hello\\2.txt");
    BufferedWriter o = new BufferedWriter(w);
    String[] arrLine;
    int lineSum=0,oddSum=0,nnSum=0;

    String strData = getData(sb.toString());
    arrLine = strData.trim().substring(1).split("#");
    for(int i=0;i<arrLine.length;i++){
    String[] arrItem = arrLine[i].trim().split(",");
    lineSum = 0;
    for(int j=0;j<arrItem.length;j++){
    int item = new Integer(arrItem[j].trim());
    lineSum += item;
    oddSum += item%2==0?0:item;
    nnSum += Math.pow(Math.sqrt(item), 2)==item?item:0;
    }
    o.write("The sum of " 
    + arrItem.length + " numbers("
    + arrLine[i] + ") is: " + lineSum);
    o.write("\r\n");
    System.out.println("The sum of " 
    + arrItem.length + " numbers("
    + arrLine[i] + ") is: " + lineSum);
    }
    o.write("The sum of odd is:"+oddSum);
    o.write("\r\n");
    o.write("The sum of n:n is:"+nnSum);

    o.close();

    }catch(Exception e){System.out.println(e.toString());}
    }
    static String getData(String str){
    Pattern p = Pattern.compile("<data>(.*)</data>");
    Matcher m = p.matcher(str);
    if(m.find()) return m.group(1);
    return "";
    }
      

  6.   

    多谢多谢,后面的算法能再简单帮我写写吗?真不好意思,初学java,有朋友给我出的这道题,说是在实践中学得能快点,我也会努力的,再次谢谢大家了啊!!!
      

  7.   

    static void writeFile(){
    try{
    FileReader r = new FileReader("C:\\my document\\hello\\1.txt");
    BufferedReader in = new BufferedReader(r);
    String s ;
    StringBuffer sb = new StringBuffer();
    while((s=in.readLine())!=null){
    sb.append(s);
    }
    in.close(); FileWriter w = new FileWriter("C:\\my document\\hello\\2.txt");
    BufferedWriter o = new BufferedWriter(w);
    String[] arrLine;
    int lineSum=0,oddSum=0,nnSum=0,sushuSum=0;

    String strData = getData(sb.toString());
    arrLine = strData.trim().substring(1).split("#");
    for(int i=0;i<arrLine.length;i++){
    String[] arrItem = arrLine[i].trim().split(",");
    lineSum = 0;
    for(int j=0;j<arrItem.length;j++){
    int item = new Integer(arrItem[j].trim());
    lineSum += item;
    oddSum += item%2==0?0:item;
    nnSum += Math.pow(Math.sqrt(item), 2)==item?item:0;
    sushuSum += checkNum(item)?item:0;
    }
    o.write("The sum of " 
    + arrItem.length + " numbers("
    + arrLine[i] + ") is: " + lineSum);
    o.write("\r\n");
    System.out.println("The sum of " 
    + arrItem.length + " numbers("
    + arrLine[i] + ") is: " + lineSum);
    }
    o.write("The sum of odd is:"+oddSum);
    o.write("\r\n");
    o.write("The sum of n:n is:"+nnSum);
    o.write("\r\n");
    o.write("The sum of sushu is:"+sushuSum);

    o.close();

    }catch(Exception e){System.out.println(e.toString());}
    }
    static String getData(String str){
    Pattern p = Pattern.compile("<data>(.*)</data>");
    Matcher m = p.matcher(str);
    if(m.find()) return m.group(1);
    return "";
    }
    static boolean checkNum(int num){
    for(int i=2;i<Math.sqrt(num);i++){
    if(num%i==0)return false;
    }
    return true;
    }
      

  8.   

    100内的素数
    public class SuShu {
    public static void main(String args[]){
    int i,j;
    int sum=0;
    for(i=2;i<100;i++)
    {
    boolean s=true;
    for(j=2;j<i;j++){
    if(i%j==0)
    s=false; }
    if(s){
    sum+=i;

    }
      }
    System.out.print(sum+" ");
    }
    }