资源预览内容
第1页 / 共16页
第2页 / 共16页
第3页 / 共16页
第4页 / 共16页
第5页 / 共16页
第6页 / 共16页
第7页 / 共16页
第8页 / 共16页
第9页 / 共16页
第10页 / 共16页
亲,该文档总共16页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
实验 1 分析成绩单一、实验目的:掌握字符输入、输出流用法。二、实验代码:Fenxi:import java.util.*;public class Fenxipublic static double getTotalScore(String s)Scanner scanner=new Scanner(s);scanner.useDelimiter(0123456789.+);double totalScore=0;while(scanner.hasNext()trydouble score=scanner.nextDouble();totalScore=totalScore+score;catch(InputMismatchException exp)String t=scanner.next();return totalScore;AnalysisResult:import java.io.*;import java.util.*;public class AnalysisResultpublic static void main(String args)File fRead=new File(score.txt);File fWrite=new File(scoreAnalysis.txt);tryWriter out= new FileWriter(fWrite,true);/以尾加方式创建指向文件 fWrite 的out 流BufferedWriter bufferWrite=new BufferedWriter(out); /创建指向 out 的bufferWrite 流Reader in=new FileReader(fRead); /创建指向文件 fRead 的 in 流BufferedReader bufferRead=new BufferedReader(in); /创建指向 in 的 bufferRead流String str=null;while (str=bufferRead.readLine()!=null)double totalScore =Fenxi.getTotalScore(str);str=str+总分:+totalScore;System.out.println(str);bufferWrite.write(str);bufferWrite.newLine();bufferRead.close();bufferWrite.close();catch(IOException e)System.out.println(e.toString();三、结果截图:四、实验分析:(1) 改进程序,使得能统计出每个学生的平均成绩。答:(2) 现在有如下格式的货物明细(文本格式)goods.txt品名:电视,length:102 cm,width:89 cm,height:56 cm.品名:轿车,length:4502 cm,width:178 cm,height:156 cm.品名:桌子,length:125 cm,width:78 cm,height:68 cm.答:CalculateVolume.javaimport java.io.*;import java.util.*;public class CalculateVolumepublic static void main(String args)File fRead=new File(goods.txt);File fWrite=new File(goodsVolume.txt);tryWriter out=new FileWriter(fWrite,true);BufferedWriter bufferWrite=new BufferedWriter(out);Reader in=new FileReader(fRead);BufferedReader bufferRead=new BufferedReader(in);String str=null;while(str=bufferRead.readLine()!=null)double s=Jisuan.getVolume(str);str=str+体积:+s+cm3;System.out.println(str);bufferWrite.write(str);bufferWrite.newLine();bufferRead.close();bufferWrite.close();catch(IOException e)System.out.println(e.toString();Jisuan.javaimport java.util.*;public class Jisuanpublic static double getVolume(String s)Scanner scanner=new Scanner(s);scanner.useDelimiter(0123456789.+);double volume=1;while(scanner.hasNext()trydouble distance=scanner.nextDouble();volume=volume*distance;catch(InputMismatchException exp)String t=scanner.next();return volume;实验 2 统计英文单词一、实验目的:掌握使用 Scanner 类解析文件。二、实验代码:Dictionary.java:import java.util.*;public class Dictionarypublic static void main(String args)Vector allWord,noSameWord;WordStatistic statistic=new WordStatistic();statistic.setFileName(hello.txt);statistic.wordStatistic();/statistic 调用 WordStatistic()方法allWord=statistic.getAllWord();noSameWord=statistic.getNoSameWord();System.out.println(共有+allWord.size()+个英文单词);System.out.println(有+noSameWord.size()+个互不相同英文单词);System.out.println(按字典顺序排列:);String s=new String noSameWord.size();for(int i=0;i allWord,noSameWord;WordStatistic statistic=new WordStatistic();statistic.setFileName(hello.txt);statistic.wordStatistic();/statistic 调用 WordStatistic()方法allWord=statistic.getAllWord();noSameWord=statistic.getNoSameWord();System.out.println(共有+allWord.size()+个英文单词);System.out.println(有+noSameWord.size()+个互不相同英文单词);System.out.println(按出现频率排列:);int count=new intnoSameWord.size();for(int i=0;icountm)String temp=noSameWord.elementAt(m);noSameWord.setElementAt(noSameWord.elementAt(n),m);noSameWord.setElementAt(temp,n);int t=countm;countm=countn;countn=t;for(int m=0;m allWord,noSameWord;File file=new File(english.txt);Scanner sc=null;String regex;WordStatistic()allWord=new Vector();noSameWord=new Vector();/regex 是由空格数字和符号(!#$&()*+,-./:;?_|)组成的正则表达式regex=sdpPunct+;trysc=new Scanner(file);/创建指向 file 的 scsc.useDelimiter(regex); /sc 调用 useDelimiter(String regex)方法,向参数传递regexcatch(IOException exp)System.out.println(exp.toString();void setFileName(String name)file=new File(name);trysc=new Scanner(file);sc.useDelimiter(regex);catch(IOException exp)System.out.println(exp.toString();public void wordStatistic()trywhile(sc.hasNext()String word=sc.next();allWord.add(word);if(!noSameWord.contains(word)noSameWord.add(word);catch(Exception e)public Vector getAllWord()return allWord;public Vector getNoSameWord()return noSameWord;三、结果截图:四、实验分析:按字典顺序输出全部不相同的单词。答:Dictionary.javaimport java.util.*;public class Dictionarypublic static void main(String args)Vector allWord,noSameWord;WordStatistic statistic=new WordStatistic();statistic.setFileName(hello.txt);statistic.wordStatistic();/statistic 调用 WordStatistic()方法allWord=statistic.getAllWord();noSameWord=statistic.getNoSameWord();System.out.println(共有+allWord.size()+个英文单词);System.out.println(有+noSameWord.size()+个互不相同英文单词);System.out.println(按字典顺序排列:);String s=new String noSameWord.size();for(int i=0;i,direi.getName();break;public static void main(String args) throws IOException
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号