您的位置:首页 > 其它

作业4.2

2016-03-29 20:34 453 查看
本次实验和 范鋆 同学结对完成。

本次程序在上次程序上稍加改动,增加搜索单词的功能,主要代码如下:

public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.println("请输入想要查找的单词");
String m=in.nextLine();
int count=0;
try{
FileReader fr1=new FileReader("Gone_with_the_wind.txt");   //读取
BufferedReader bf1=new BufferedReader(fr1);
String str=null;

while( (str=bf1.readLine())!=null)
{
str=str.replace("\\n", "");
str=str.toLowerCase();
str=str.replaceAll("[\\pP‘’“”]", "");
String[] str1=str.split(" ");

for(int i=0;i<str1.length;i++)
{
if(m.equals(str1[i]))count++;
}

}
fr1.close(); bf1.close();
}catch(Exception e){e.printStackTrace();}

try{
FileWriter fw=new FileWriter("Result1.txt");
BufferedWriter bw=new BufferedWriter(fw);
bw.write(m+"="+count);
bw.close();fw.close();
}catch(Exception e){e.printStackTrace();}
}
}


实验数据:

我搜索了3个单词 that with have,并和上次结果对比









结果一致

github地址:https://github.com/sunhongyi1996/repository
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: