【2017年整理】java 实验报告

上传人:爱****1 文档编号:941881 上传时间:2017-05-23 格式:DOC 页数:16 大小:477KB
返回 下载 相关 举报
【2017年整理】java 实验报告_第1页
第1页 / 共16页
【2017年整理】java 实验报告_第2页
第2页 / 共16页
【2017年整理】java 实验报告_第3页
第3页 / 共16页
【2017年整理】java 实验报告_第4页
第4页 / 共16页
【2017年整理】java 实验报告_第5页
第5页 / 共16页
点击查看更多>>
资源描述

《【2017年整理】java 实验报告》由会员分享,可在线阅读,更多相关《【2017年整理】java 实验报告(16页珍藏版)》请在金锄头文库上搜索。

1、实验 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;ca

2、tch(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)

3、;/以尾加方式创建指向文件 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

4、 =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 c

5、m,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(goodsV

6、olume.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.p

7、rintln(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.

8、+);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 mai

9、n(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.pri

10、ntln(有+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()

11、;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),

12、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 Scanne

13、r(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()

14、;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 j

15、ava.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

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 研究报告 > 综合/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号