doublefrequency=(1.0*count[m])/allWord.size();
System.out.printf("%s:
%-7.3f",noSameWord.elementAt(m),frequency);
}
}
}
实验3密码流
1.模板代码
PassWord.java
importjava.io.*;
publicclassPassWord{
publicstaticvoidmain(String[]args){
booleansuccess=false;
intcount=0;
Consolecons;
char[]passwd;
cons=System.console();
while(true){
System.out.print("输入密码:
");
passwd=cons.readPassword();
count++;
Stringpassword=newString(passwd);
if(password.equals("tiger123")){
success=true;
System.out.println("您第"+count+"次输入的密码正确!
");
break;
}else{
System.out.println("您第"+count+"次输入的密码"+password+"不正确!
");
}
if(count==3){
System.out.println("您"+count+"次输入的密码都不正确!
");
System.exit(0);
}
}
if(success){
try{
Filefile=newFile("D:
\\score.txt");
FileReaderinOne=newFileReader(file);
BufferedReaderinTow=newBufferedReader(inOne);
Strings=null;
while((s=inTow.readLine())!
=null){
System.out.println(s);
}
inOne.close();
inTow.close();
}catch(FileNotFoundExceptionfe){
System.out.println(fe.toString());
}
catch(IOExceptionioe){
System.out.println(ioe.toString());
}
}
}
}
实验4学读汉字
1.模板代码
ChineseCharacters.java
importjava.io.*;
importjava.util.StringTokenizer;
publicclassChineseCharacters
{publicStringBuffergetChinesecharacters(Filefile)
{StringBufferhanzi=newStringBuffer();
try{FileReaderinOne=【代码1】//创建指向文件f的inOne的对象
BufferedReaderinTwo=【代码2】//创建指向文件inOne的inTwo的对象
Strings=null;
inti=0;
while((s=【代码3】)!
=null)//inTwo读取一行
{StringTokenizertokenizer=newStringTokenizer(s,",'\n'");
while(tokenizer.hasMoreTokens())
{hanzi.append(tokenizer.nextToken());
}
}
}
catch(Exceptione){}
returnhanzi;
}
}
StudyFrame.java
importjava.awt.*;
importjava.awt.event.*;
importjava.io.*;
importjavax.sound.sampled.*;
publicclassStudyFrameextendsFrameimplementsItemListener,ActionListener,Runnable
{ChineseCharacterschinese;
Choicechoice;
ButtongetCharacters,voiceCharacters;
LabelshowCharacters;
StringBuffertrainedChinese=null;
Clipclip=null;
ThreadvoiceThread;
intk=0;
PanelpCenter;
CardLayoutmycard;
TextAreatextHelp;
MenuBarmenubar;
Menumenu;
MenuItemhelp;
publicStudyFrame()
{chinese=newChineseCharacters();
choice=newChoice();
choice.add("training1.txt");
choice.add("training2.txt");
choice.add("training3.txt");
showCharacters=newLabel("",Label.CENTER);
showCharacters.setFont(newFont("宋体",Font.BOLD,72));
showCharacters.setBackground(Color.green);
getCharacters=newButton("下一个汉字");
voiceCharacters=newButton("发音");
voiceThread=newThread(this);
choice.addItemListener(this);
voiceCharacters.addActionListener(this);
getCharacters.addActionListener(this);
PanelpNorth=newPanel();
pNorth.add(newLabel("选择一个汉字字符组成的文件"));
pNorth.add(choice);
add(pNorth,BorderLayout.NORTH);
PanelpSouth=newPanel();
pSouth.add(getCharacters);
pSouth.add(voiceCharacters);
add(pSouth,BorderLayout.SOUTH);
pCenter=newPanel();
mycard=newCardLayout();
pCenter.setLayout(mycard);
textHelp=newTextArea();
pCenter.add("hanzi",showCharacters);
pCenter.add("help",textHelp);
add(pCenter,BorderLayout.CENTER);
menubar=newMenuBar();
menu=newMenu("帮助");
help=newMenuItem("关于学汉字");
help.addActionListener(this);
menu.add(help);
menubar.add(menu);
setMenuBar(menubar);
setSize(350,220);
setVisible(true);
addWindowListener(newWindowAdapter()
{publicvoidwindowClosing(WindowEvente)
{System.exit(0);
}
});
validate();
}
publicvoiditemStateChanged(ItemEvente)
{StringfileName=choice.getSelectedItem();
Filefile=newFile(fileName);
trainedChinese=chinese.getChinesecharacters(file);
k=0;
mycard.show(pCenter,"hanzi");
}
publicvoidactionPerformed(ActionEvente)
{if(e.getSource()==getCharacters)
{if(trainedChinese!
=null)
{charc=trainedChinese.charAt(k);
k++;
if(k>=trainedChinese.length())
k=0;
showCharacters.setText(""+c);
}
else
{showCharacters.setText("请选择一个汉字字符文件");
}
}
if(e.getSource()==voiceCharacters)
{if(!
(voiceThread.isAlive()))
{voiceThread=newThread(this);
}
try{voiceThread.start();
}
catch(Exceptionexp){}
}
if(e.getSource()==help)
{mycard.show(pCenter,"help");
try{FilehelpFile=newFile("help.txt");
FileReaderinOne=【代码4】//创建指向文件helpFile的inOne的对象
BufferedReaderinTwo=【代码5】//创建指向文件inOne的inTwo的对象
Strings=null;
while((s=inTwo.readLine())!
=null)
{textHelp.append(s+"\n");
}
inOne.close();
inTwo.close();
}
catch(IOExceptionexp){}
}
}
publicvoidrun()
{voiceCharacters.setEnabled(false);
try{if(clip!
=null)
{clip.close()
}
clip=AudioSystem.getClip();
FilevoiceFile=newFile(showCharacters.getText().trim()+".wav");
clip.open(AudioSystem.getAudioInputStream(voiceFile));
}
catch(Exceptionexp){}
clip.start();
voiceCharacters.set