Java面向?qū)ο蟪绦蛟O(shè)計實驗-實驗八輸入輸出流_第1頁
Java面向?qū)ο蟪绦蛟O(shè)計實驗-實驗八輸入輸出流_第2頁
Java面向?qū)ο蟪绦蛟O(shè)計實驗-實驗八輸入輸出流_第3頁
Java面向?qū)ο蟪绦蛟O(shè)計實驗-實驗八輸入輸出流_第4頁
Java面向?qū)ο蟪绦蛟O(shè)計實驗-實驗八輸入輸出流_第5頁
已閱讀5頁,還剩8頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、. *科技大學計通學院Java面向?qū)ο蟪绦蛟O(shè)計實驗實驗八輸入輸出流學生:*:班級:指導教師:專業(yè):計算機科學與技術(shù)提交日期:年月日實驗報告容實驗?zāi)康恼莆兆址斎?、輸出流用法;掌握使用Scanner類解析文件;掌握Console流的使用。實驗容實驗教材-第12章實驗1、2、3字符輸入輸出流;Scanner類和Console類。要求:完善程序,給出實驗結(jié)果截圖;完成試驗后練習。程序代碼及運行結(jié)果:實驗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。運行結(jié)果如圖1所示圖1實驗2 統(tǒng)計英文單詞/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(按出現(xiàn)的頻率排列:); 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); 運行結(jié)果如圖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。運行結(jié)果如圖3所示圖3實驗后的練習:實驗1有如下格式的成績單文本格式score.t*t。:三,數(shù)學72分,物理67分,英語70分。:四,數(shù)學92分,物理98分,英語88分。:周五,數(shù)學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:三,數(shù)學72分,物理67分,英語70分。:四,數(shù)學92分,物理98分,英語88分。:周五,數(shù)學68分,物理80分,英語77分。運行結(jié)果如圖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調(diào)用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+ );運行結(jié)果如圖5所示圖5實驗3 編寫一個程序,程序運行時,要求用戶輸入的密碼是:hello。如果用戶輸入了正確的密碼。程序?qū)⑤敵瞿愫?,歡送你!。程序允許用戶2次輸入的密碼不正確,一旦超過2次,程序?qū)⒘⒖掏顺觥?PassWord.javaimport java.io.*; import java.util.Scanner; public class PassWord pu

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論