Java面向对象程序设计实验-实验八输入输出流_第1页
Java面向对象程序设计实验-实验八输入输出流_第2页
Java面向对象程序设计实验-实验八输入输出流_第3页
Java面向对象程序设计实验-实验八输入输出流_第4页
Java面向对象程序设计实验-实验八输入输出流_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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

2、tatic void main(String args) File fRead=new File(score.t*t); File fWrite=new File(scoreAnalysis.t*t); try Writer out=new FileWriter(fWrite); BufferedWriter bufferWrite=new BufferedWriter(out); Reader in=new FileReader(fRead); BufferedReader bufferRead=new BufferedReader(in); String str=null; while(s

3、tr=bufferRead.readLine()!=null) double totalScore=Fen*i.getTotalScore(str); str=str+总成绩:+totalScore; System.out.println(str); bufferWrite.write(str); bufferWrite.newLine(); bufferRead.close(); bufferWrite.close(); catch(IOE*ception e) System.out.println(e.toString(); /Fen*i.javaimport java.util.*; p

4、ublic class Fen*i public static double getTotalScore(String s) String rege* = 0123456789.; String digitMess = s.replaceAll(rege*,*); StringTokenizer fen*i = new StringTokenizer(digitMess,*); double totalScore=0; while(fen*i.hasMoreTokens() double score=Double.parseDouble(fen*i.ne*tToken(); totalScor

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

6、String rege*; WordStatistic () allWord=new Vector(); noSameWord=new Vector(); rege*=sdpPunct+;/正则表达式 try sc=new Scanner(file); sc.useDelimiter(rege*); catch(IOE*ception e*p) System.out.println(e*p.toString(); void setFileName(String name) file=new File(name); try sc=new Scanner(file); sc.useDelimite

7、r(rege*); catch(IOE*ception e*p) System.out.println(e*p.toString(); void WordStatistic() try while(sc.hasNe*t() String word=sc.ne*t(); allWord.add(word); if(!noSameWord.contains(word) noSameWord.add(word); catch(E*ception e) public VectorgetAllWord() return allWord; public VectorgetNoSameWord() retu

8、rn noSameWord; /OutputWordMess.javaimport java.util.*;public class OutputWordMess public static void main(String args) Vector allWord,noSameWord; WordStatistic statistic=new WordStatistic(); statistic.setFileName(hello.t*t); statistic.WordStatistic(); allWord=statistic.getAllWord(); noSameWord=stati

9、stic.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); for(int j=0;jallWord.size();j+) String s2=all

10、Word.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=countn; countn=t; for(int m=0;mnoSameWord.size();m+)

11、 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 count=0;Console cons;char passwd;cons=System.console()

12、;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;elseSystem.out.println(您第+count+次输入的密码+password+不正确!);if(count=3)System.out.println(您+count+次输入的密码都不正确!);Sy

13、stem.e*it(0);if(success)File file=new File(score1.t*t);tryFileReader inOne=new FileReader(file);BufferedReader inTow=new BufferedReader(inOne);String s=null;while(s=inTow.readLine()!=null)System.out.println(s);inOne.close();inTow.close();catch(IOE*ception ioe) /score.t*t:三,抓举成绩 106kg,挺举 189kg。:四,抓举成

14、绩 108kg,挺举 186kg。:周五,抓举成绩 112kg,挺举 190kg。运行结果如图3所示图3实验后的练习:实验1有如下格式的成绩单文本格式score.t*t。:三,数学72分,物理67分,英语70分。:四,数学92分,物理98分,英语88分。:周五,数学68分,物理80分,英语77分。要求按行读入取成绩单,并在该行的后面尾加上该同学的总成绩,然后再将该行写入到一个名字为scoreAnalysis.t*t的文件中。/AnalysisResult.javaimport java.io.*; import java.util.*;public class AnalysisResult p

15、ublic static void main(String args) File fRead=new File(score.t*t); File fWrite=new File(scoreAnalysis.t*t); try Writer out=new FileWriter(fWrite,true); BufferedWriter bufferWrite=new BufferedWriter(out); Reader in=new FileReader(fRead); BufferedReader bufferRead=new BufferedReader(in); String str=n

16、ull; while(str=bufferRead.readLine()!=null) double totalScore=Fen*i.getTotalScore(str); str=str+总分:+totalScore; System.out.println(str); bufferWrite.write(str); bufferWrite.newLine(); bufferRead.close(); bufferWrite.close(); catch(IOE*ception e) System.out.println(e.toString(); /Fen*i.javaimport jav

17、a.util.*;public class Fen*i public static double getTotalScore(String s) Scanner scanner=new Scanner(s); scanner.useDelimiter(0123456789.+); double totalScore=0; while(scanner.hasNe*t() try double score=scanner.ne*tDouble(); totalScore=totalScore+score; catch(InputMismatchE*ception e*p) String t=sca

18、nner.ne*t(); return totalScore; /score.t*t:三,数学72分,物理67分,英语70分。:四,数学92分,物理98分,英语88分。:周五,数学68分,物理80分,英语77分。运行结果如图4所示图4实验2 按字典序输出全部不一样的单词。/Dictionary.javaimport java.util.*;public class Dictionary public static void main(String args)Vector allWord,noSameWord;WordStatistic statistic=new WordStatistic()

19、;statistic.setFileName(hello.t*t);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

20、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 编写一个程序,程序运行时,要求用户输入的密码是:hello。如果用户输入了正确的密码。程序将输出你好,欢送你!。程序允许用户2次输入的密码不正确,一旦超过2次,程序将立刻退出。/PassWord.javaimport java.io.*; import java.util.Scanner; public class PassWord pu

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论