ImageVerifierCode 换一换
格式:DOCX , 页数:13 ,大小:61.50KB ,
资源ID:6883946      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/6883946.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Java面向对象程序设计实验实验八输入输出流.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

Java面向对象程序设计实验实验八输入输出流.docx

1、Java面向对象程序设计实验实验八输入输出流广西科技大学计通学院Java面向对象程序设计实验实验八 输入输出流 学生: 学 号: 班 级: 指导老师: 专 业:计算机科学与技术提交日期:年 月日实验报告容1实验目的掌握字符输入、输出流用法;掌握使用Scanner类解析文件;掌握Console流的使用。2实验容实验教材-第12章实验1、2、3字符输入输出流;Scanner类和Console类。 要求: 完善程序,给出实验结果截图;完成试验后练习。3程序代码及运行结果:实验1 举重成绩单/ AnalysisResult.javaimport java.io.*; import java.util.

2、*;public class AnalysisResult public static void main(String args) File fRead=new File(score.txt); File fWrite=new File(scoreAnalysis.txt); try Writer out=new FileWriter(fWrite); BufferedWriter bufferWrite=new BufferedWriter(out); Reader in=new FileReader(fRead); BufferedReader bufferRead=new Buffer

3、edReader(in); 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.toStri

4、ng(); / Fenxi.javaimport java.util.*; public class Fenxi public static double getTotalScore(String s) String regex = 0123456789.; String digitMess = s.replaceAll(regex,*); StringTokenizer fenxi = new StringTokenizer(digitMess,*); double totalScore=0; while(fenxi.hasMoreTokens() double score=Double.p

5、arseDouble(fenxi.nextToken(); totalScore=totalScore+score; return totalScore; / score.txt:三, 抓举成绩 106kg, 挺举 189kg。:四, 抓举成绩 108kg, 挺举 186kg。:周五, 抓举成绩 112kg, 挺举 190kg。运行结果如图1所示图1实验2 统计英文单词/ WordStatistic.javaimport java.io.*; import java.util.*;public class WordStatistic Vector allWord,noSameWord; Fil

6、e file=new File(english.txt); Scanner sc=null; String regex; WordStatistic () allWord=new Vector(); noSameWord=new Vector(); regex=sdpPunct+;/正则表达式 try sc=new Scanner(file); sc.useDelimiter(regex); catch(IOException exp) System.out.println(exp.toString(); void setFileName(String name) file=new File(

7、name); try sc=new Scanner(file); sc.useDelimiter(regex); catch(IOException exp) System.out.println(exp.toString(); void WordStatistic() try while(sc.hasNext() String word=sc.next(); allWord.add(word); if(!noSameWord.contains(word) noSameWord.add(word); catch(Exception e) public VectorgetAllWord() re

8、turn allWord; public VectorgetNoSameWord() return noSameWord; / OutputWordMess.javaimport java.util.*;public class OutputWordMess public static void main(String args) Vector allWord,noSameWord; WordStatistic statistic=new WordStatistic(); statistic.setFileName(hello.txt); statistic.WordStatistic();

9、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;inoSameWord.size();i+) String s1=noSameWord.elementAt(i

10、); for(int j=0;jallWord.size();j+) String s2=allWord.elementAt(j); if(s1.equals(s2) counti+; for(int m=0;mnoSameWord.size();m+) for(int n=m+1;ncountm) String temp=noSameWord.elementAt(m); noSameWord.setElementAt(noSameWord.elementAt(n), m); noSameWord.setElementAt(temp, n); int t=countm; countm=coun

11、tn; countn=t; for(int m=0;mnoSameWord.size();m+) double frequency=(1.0*countm)/allWord.size(); System.out.printf(%s:%-7.3f, noSameWord.elementAt(m),frequency); 运行结果如图2所示图2实验3 密码流/ PassWord.javaimport java.io.*;public class PassWord public static void main(String args) boolean success=false; int coun

12、t=0; Console cons; char passwd; cons=System.console(); while(true) System.out.print(输入密码:); passwd=cons.readPassword(); count+; String password= new String(passwd); if(password.equals(tiger123) success=true; System.out.println(您第+count+次输入的密码正确!); break; else System.out.println(您第+count+次输入的密码+passw

13、ord+不正确!); if(count=3) System.out.println(您+count+次输入的密码都不正确!); System.exit(0); if(success) File file=new File(score1.txt); try FileReader inOne=new FileReader(file); BufferedReader inTow=new BufferedReader(inOne); String s=null; while(s=inTow.readLine()!=null) System.out.println(s); inOne.close();

14、inTow.close(); catch(IOException ioe) / score.txt:三, 抓举成绩 106kg, 挺举 189kg。:四, 抓举成绩 108kg, 挺举 186kg。:周五, 抓举成绩 112kg, 挺举 190kg。运行结果如图3所示图34实验后的练习:实验1 有如下格式的成绩单(文本格式)score.txt。:三,数学72分,物理67分,英语70分。:四,数学92分,物理98分,英语88分。:周五,数学68分,物理80分,英语77分。要求按行读入取成绩单,并在该行的后面尾加上该同学的总成绩,然后再将该行写入到一个名字为scoreAnalysis.txt的文件

15、中。/ AnalysisResult.javaimport java.io.*; import java.util.*;public class AnalysisResult public static void main(String args) File fRead=new File(score.txt); File fWrite=new File(scoreAnalysis.txt); try Writer out=new FileWriter(fWrite,true); BufferedWriter bufferWrite=new BufferedWriter(out); Reader

16、 in=new FileReader(fRead); BufferedReader bufferRead=new BufferedReader(in); 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(); bufferW

17、rite.close(); catch(IOException e) System.out.println(e.toString(); / Fenxi.javaimport java.util.*;public class Fenxi public static double getTotalScore(String s) Scanner scanner=new Scanner(s); scanner.useDelimiter(0123456789.+); double totalScore=0; while(scanner.hasNext() try double score=scanner

18、.nextDouble(); totalScore=totalScore+score; catch(InputMismatchException exp) String t=scanner.next(); return totalScore; / score.txt:三,数学72分,物理67分,英语70分。:四,数学92分,物理98分,英语88分。:周五,数学68分,物理80分,英语77分。运行结果如图4所示图4实验2 按字典序输出全部不相同的单词。/ Dictionary.javaimport java.util.*;public class Dictionary public static

19、 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()+个英文单词);

20、 System.out.println(有+noSameWord.size()+个互不相同英文单词); System.out.println(按字典顺序排列:); String s=new String noSameWord.size(); for(int i=0;inoSameWord.size();i+) si=noSameWord.elementAt(i); Arrays.sort(s); for(int i=0;inoSameWord.size();i+) System.out.println(si+ ); 运行结果如图5所示图5实验3 编写一个程序,程序运行时,要求用户输入的密码是:

21、hello。如果用户输入了正确的密码。程序将输出“你好,欢迎你!”。程序允许用户2次输入的密码不正确,一旦超过2次,程序将立刻退出。/ PassWord.javaimport java.io.*; import java.util.Scanner; public class PassWord public static void main(String args) int count=0; Console cons; String passwd; Scanner sc=new Scanner(System.in);/cons=System.console(); while(true) Syst

22、em.out.print(输入密码:); passwd=sc.next();/ count+; String password=new String (passwd); if(password.equals(hello) System.out.println(你好,欢迎你!); break; else System.out.println(您第+count+次输入的密+password+不正确); if(count=3) System.out.println(您+count+次输入的密码都不正确); System.exit(0); 运行结果如图6所示图65实验心得和体会这次实验中,我感觉比较难理解和掌握的就是Console类了,在实验3 密码流的代码在eclipse中运行出错如下图所示:就是输入不了密码,后来通过找寻解决方法,才知道了在JDK 6中java.io.Console类专用来访问基于字符的控制台设备,然后使用cmd命令提示符来运行就可以输入密码和显示结果了。通过这次实验,大概了解了字符输入、输出流用法,如何去使用Scanner类解析文件,对Console类的使用有个大概的认知。总而言之,这次实验收获蛮大的,敲代码的效率比以往提高了,出错率也少了。

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1