package zy03; import java.io.*; import java.util.*; public class ceshi { private static String str=""; private static Scanner sc=new Scanner(System.in); private static BufferedReader br=null; private static String a[]=new String[1000000]; private static String c[]=new String[10000000]; private static int b[]=new int[1000000]; private static int l=0; private static int l1=0; private static int n=0; private static int j=0; //private static Boolean false; public static void cunfang() throws IOException {//将单词存到数组a while(str!=null) { int i=0; str=str.toLowerCase(); //把大写改成小写 for(i=0;i<str.length();i++) { if((str.charAt(i)>96&&str.charAt(i)<123)) { a[j]=a[j]+str.charAt(i); } if(str.charAt(i)==' '||str.charAt(i)==','||str.charAt(i)=='.') { if(!a[j].equals("")) { j=j+1; a[j]=""; } } } str=br.readLine(); } l=j; } public static void show() {//显示 for(int k=0;k<n;k++) { System.out.println(c[k]+"\t"+b[k]); } } public static void paixu() {//排序 int t3=0; int t2=0; String sr=""; for(int i=0;i<l-1;i++) { t3=i; for(int j=i+1;j<l1;j++) { if(b[t3]<b[j]) { t3=j; } } if(t3!=i) { t2=b[i]; b[i]=b[t3]; b[t3]=t2; sr=c[i]; c[i]=c[t3]; c[t3]=sr; } } } public static void quchong() {//去重 for(int k=0;k<l;k++) { b[k]=0; } c[0]=a[0]; int tt=1; Boolean rt=true; for(int i=1;i<l;i++) { rt=false; for(int j=0;j<tt;j++) { if(a[i].equals(c[j])) { rt=true; break; } } if(!rt) { c[tt]=a[i]; tt++; } } l1=tt; for(int i=0;i<l1;i++) { for(int j=0;j<l;j++) { if(c[i].equals(a[j])) { b[i]++; } } } } public static void Readfile() { File file=new File("D://大二java//飘.txt"); try { InputStreamReader read = new InputStreamReader(new FileInputStream(file),"UTF-8"); br=new BufferedReader(read); str=br.readLine(); cunfang(); br.close(); read.close(); } catch(IOException e) { System.out.println("读取失败!"); e.printStackTrace(); } } public static void Writefile() throws IOException { File file=new File("D://大二java//飘.txt"); if(!file.exists()) file.createNewFile(); FileWriter write = new FileWriter(file,true); BufferedWriter out=new BufferedWriter(write); for(int i=0;i<n;i++) { StringBuffer sb=new StringBuffer(); out.write("这是第"+(i+1)+"个: "); out.write(c[i]+"\t"+b[i]); out.write("\r\n"); } out.close(); } public static void main(String[] args) throws IOException { System.out.println("请输入您要统计前几个最常出现的单词:"); n=sc.nextInt(); a[0]=""; Readfile(); quchong(); paixu(); show(); Writefile(); } }