单词 统计续
这次的单词统计是上次作业的加强版,又提出了一些需求,可以自由控制输出前n个单词,既可以统计字母,又可以统计单词,我将他们做成了一个功能菜单,如下:
1:统计字母数量及占比
2:统计单词数量及占比
3:统计前n个单词数量及占比
按0退出
请选择:
当然,我们代码的功能必须基于菜单来做,最简单的就是2和3,因为我们在上次作业中已经统计了单词数量和占比,只需要在最后一次输出前n个单词数量的时候,要求用户输入一个数,在循环中把这个数作为循环终止的条件即可。至于字母数量和占比,其实大同小异,就是split分割方法不同,而且要把空格,逗号等字符去掉,统计方法其实一样。
代码如下:
1 package piao; 2 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.io.FileReader; 8 import java.io.IOException; 9 import java.io.PrintStream; 10 import java.util.ArrayList; 11 import java.util.List; 12 import java.util.Scanner; 13 14 15 class letter 16 { 17 private char l; 18 private int count; 19 20 public char getL() { 21 return l; 22 } 23 public void setL(char l) { 24 this.l = l; 25 } 26 public int getCount() { 27 return count; 28 } 29 public void setCount(int count) { 30 this.count = count; 31 } 32 33 public letter() {}; 34 public letter(char l,int count) 35 { 36 this.l=l; 37 this.count=count; 38 } 39 } 40 class word 41 { 42 private String value; 43 private int count; 44 45 public String getValue() { 46 return value; 47 } 48 public void setValue(String value) { 49 this.value = value; 50 } 51 public int getCount() { 52 return count; 53 } 54 public void setCount(int count) { 55 this.count = count; 56 } 57 public word() {}; 58 public word(String value,int count) 59 { 60 this.value=value; 61 this.count=count; 62 } 63 } 64 65 public class Piao { 66 67 public static void main(String[] args)throws FileNotFoundException, IOException { 68 // TODO 自动生成的方法存根 69 Interface(); 70 Scanner scan=new Scanner(System.in); 71 System.out.println("请选择:"); 72 int num=scan.nextInt(); 73 while(num!=0) 74 { 75 switch(num) 76 { 77 case 1:LetterList();break; 78 case 2:WordList();break; 79 case 3:System.out.println("输入数量n");int n=scan.nextInt();WordList(n);break; 80 } 81 Interface(); 82 System.out.println("请选择:"); 83 num=scan.nextInt(); 84 } 85 } 86 87 public static void Interface()//主界面 88 { 89 System.out.println("1:统计字母数量及占比"); 90 System.out.println("2:统计单词数量及占比"); 91 System.out.println("3:统计前n个单词数量及占比"); 92 System.out.println("按0退出"); 93 } 94 95 public static void LetterList()throws FileNotFoundException, IOException//统计字母 96 { 97 File file=new File("F:/java开发/piao.txt"); 98 BufferedReader br=new BufferedReader(new FileReader(file)); 99 FileOutputStream out=new FileOutputStream("F:/java开发/jieguo.txt"); 100 PrintStream p=new PrintStream(out);//文件写入流 101 102 String s; 103 int i=0; 104 double b=0; 105 int j=0; 106 107 double sum=0; 108 int count=0; 109 s=br.readLine(); 110 char [] strArr1 = s.toCharArray();//转化为单个字符 111 112 for(i=0;i<strArr1.length;i++) 113 { 114 if(strArr1[i]!=' '||strArr1[i]!=','||strArr1[i]!='.') 115 sum++; 116 }//求全部字母数 117 for(char ch='a';ch<'z';ch++) 118 { 119 count=0;//计数器 120 for(i=0;i<strArr1.length;i++) 121 { 122 if(ch==strArr1[i]||strArr1[i]==ch-32) 123 count++; 124 } 125 if(count!=0) 126 b=count/sum; 127 System.out.print("字符中"+ch+"有"+count+"个"); 128 System.out.println("占比:"+String.format("%.2f", b)); 129 130 } 131 132 } 133 134 public static void WordList()throws FileNotFoundException, IOException//统计单词 135 { 136 File file=new File("F:/java开发/piao.txt"); 137 File file2=new File("F:/java开发/notuse.txt"); 138 BufferedReader br=new BufferedReader(new FileReader(file)); 139 BufferedReader br2=new BufferedReader(new FileReader(file2)); 140 FileOutputStream out=new FileOutputStream("F:/java开发/jieguo.txt"); 141 PrintStream p=new PrintStream(out);//文件写入流 142 143 String s; 144 String ss; 145 int i=0; 146 double b=0; 147 int j=0; 148 List<String> list=new ArrayList<String>(); 149 double sum=0; 150 int count=0; 151 s=br.readLine(); 152 ss=br2.readLine(); 153 String []s1=s.split(" ");//以空格为分割划分每个单词 154 String []s2=ss.split(" "); 155 156 157 word []w=new word[s1.length]; 158 159 for(i=0;i<s1.length;i++) 160 { 161 162 count=0; 163 for(j=0;j<s1.length;j++) 164 { 165 if(s1[i].equals(s1[j])) 166 { 167 count++; 168 } 169 170 } 171 172 w[i]=new word(s1[i],count); 173 } 174 175 word temp=new word(); 176 for(i=0;i<s1.length;i++) 177 { 178 for(j=0;j<s1.length-1;j++) 179 { 180 if(w[j].getCount()<w[j+1].getCount()) 181 { 182 temp=w[j]; 183 w[j]=w[j+1]; 184 w[j+1]=temp; 185 } 186 } 187 188 } 189 list.add(w[0].getValue()); 190 for(i=0;i<w.length;i++) 191 { 192 boolean bool=true; 193 for(int m=0;m<s2.length;m++) 194 { 195 if(w[i].getValue().equals(s2[m])) 196 { 197 bool=false; 198 } 199 } 200 if(list.contains(w[i].getValue())) 201 continue; 202 else 203 { 204 if(bool) 205 { 206 System.out.println(w[i].getValue()+":"+w[i].getCount()); 207 } 208 list.add(w[i].getValue()); 209 } 210 211 } 212 213 } 214 215 public static void WordList(int n)throws FileNotFoundException, IOException//统计单词 216 {File file=new File("F:/java开发/piao.txt"); 217 BufferedReader br=new BufferedReader(new FileReader(file)); 218 FileOutputStream out=new FileOutputStream("F:/java开发/jieguo.txt"); 219 PrintStream p=new PrintStream(out);//文件写入流 220 221 String s; 222 int i=0; 223 double b=0; 224 int j=0; 225 List<String> list=new ArrayList<String>(); 226 double sum=0; 227 int count=0; 228 s=br.readLine(); 229 String []s1=s.split(" ");//以空格为分割划分每个单词 230 231 232 233 word []w=new word[s1.length]; 234 235 for(i=0;i<s1.length;i++) 236 { 237 238 count=0; 239 for(j=0;j<s1.length;j++) 240 { 241 if(s1[i].equals(s1[j])) 242 { 243 count++; 244 } 245 246 } 247 248 w[i]=new word(s1[i],count); 249 } 250 251 word temp=new word(); 252 for(i=0;i<s1.length;i++) 253 { 254 for(j=0;j<s1.length-1;j++) 255 { 256 if(w[j].getCount()<w[j+1].getCount()) 257 { 258 temp=w[j]; 259 w[j]=w[j+1]; 260 w[j+1]=temp; 261 } 262 } 263 264 } 265 list.add(w[0].getValue()); 266 int nn=0; 267 for(i=0;i<s1.length;i++) 268 { 269 if(nn<n) 270 { 271 if(list.contains(w[i].getValue())) 272 continue; 273 else 274 { 275 System.out.println(w[i].getValue()+":"+w[i].getCount()); 276 list.add(w[i].getValue()); 277 nn++; 278 } 279 } 280 else 281 break; 282 } 283 284 } 285 }