当前位置:文档之家› 《Java面向对象程序设计》实验-实验八(输入输出流)

《Java面向对象程序设计》实验-实验八(输入输出流)

广西科技大学计通学院《Java面向对象程序设计》实验实验八输入输出流学生姓名:×××学号:×××班级:×××指导老师:×××专业:计算机科学与技术提交日期:×××年××月××日实验报告内容1.实验目的掌握字符输入、输出流用法;掌握使用Scanner类解析文件;掌握Console流的使用。

2.实验内容实验教材-第12章实验1、2、3字符输入输出流;Scanner类和Console类。

要求:完善程序,给出实验结果截图;完成试验后练习。

3.程序代码及运行结果:实验1 举重成绩单;import .*;public class AnalysisResult {public static void main(String[] args) {File fRead=new File("");File fWrite=new File("");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((str=())!=null){double totalScore=(str);str=str+"总成绩:"+totalScore;(str);();}();();}catch(IOException e){}}};public class Fenxi {public static double getTotalScore(String s) { String regex = "[^09.]";String digitMess = (regex,"*");StringTokenizer fenxi = newStringTokenizer(digitMess,"*");double totalScore=0;while()){double score=());totalScore=totalScore+score;}return totalScore;}};import .*;public class WordStatistic {Vector<String> allWord,noSameWord;File file=new File("");Scanner sc=null;String regex;WordStatistic (){allWord=new Vector<String>();noSameWord=new Vector<String>();regex="[\\s\\d\\p{Punct}]+";;public class OutputWordMess {public static void main(String[] args) { Vector<String> allWord,noSameWord; WordStatistic statistic=new WordStatistic();("");();allWord=();noSameWord=();"共有"+()+"个英文单词");"有"+()+"个互不相同英文单词");"按出现的频率排列:");int count[]=new int[()];for(int i=0;i<();i++){String s1=(i);for(int j=0;j<();j++){String s2=(j);if(s2))count[i]++;}}for(int m=0;m<();m++){for(int n=m+1;n<();n++){if(count[n]>count[m]){String temp=(m);(n), m);(temp, n);int t=count[m];count[m]=count[n];count[n]=t;}}}for(int m=0;m<();m++){double frequency=*count[m])/(); "%s:%", (m),frequency);}}}运行结果如图2所示图2实验3 密码流;public class PassWord {public static void main(String[] args) { boolean success=false;int count=0;Console cons;char[] passwd;cons=();while(true){"输入密码:");passwd=();count++;String password= new String(passwd);if("tiger123")){success=true;"您第"+count+"次输入的密码正确!");break;}else{"您第"+count+"次输入的密码"+password+"不正确!");}if(count==3){"您"+count+"次输入的密码都不正确!");(0);}}if(success){File file=new File("");try{FileReader inOne=new FileReader(file);BufferedReader inTow=new BufferedReader(inOne);String s=null;while((s=())!=null){}();();}catch(IOException ioe){}}}};import .*;public class AnalysisResult {public static void main(String[] args) {File fRead=new File("");File fWrite=new File("");try{Writer 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=())!=null){double totalScore=(str);str=str+"总分:"+totalScore;(str);();}();();}catch(IOException e){}}};public class Fenxi {public static double getTotalScore(String s) { Scanner scanner=new Scanner(s);("[^09.]+");double totalScore=0;while()){try{double score=();totalScore=totalScore+score;}catch(InputMismatchException exp){String t=();}}return totalScore;}};public class Dictionary {public static void main(String args[]){Vector<String> allWord,noSameWord;WordStatistic statistic=new WordStatistic();("");();;importpublic class PassWord{public static void main(String args[]){ int count=0;Console cons;String passwd;Scanner sc=new Scanner;//cons=();while(true){"输入密码:");passwd=();//count++;String password=new String (passwd);if("hello")){"你好,欢迎你!");break;}else{"您第"+count+"次输入的密"+password+"不正确");}if(count==3){"您"+count+"次输入的密码都不正确");(0);}}}}运行结果如图6所示图64.实验心得和体会这次实验中,我感觉比较难理解和掌握的就是Console类了,在实验3 密码流的代码在eclipse中运行出错如下图所示:就是输入不了密码,后来通过找寻解决方法,才知道了在JDK 6中类专用来访问基于字符的控制台设备,然后使用cmd命令提示符来运行就可以输入密码和显示结果了。

通过这次实验,大概了解了字符输入、输出流用法,如何去使用Scanner类解析文件,对Console类的使用有个大概的认知。

总而言之,这次实验收获蛮大的,敲代码的效率比以往提高了,出错率也少了。

11。

相关主题