11.12课堂测试1
作者:互联网
1 package practice; 2 3 import java.io.*; 4 import java.util.*; 5 import java.text.*; 6 import java.io.BufferedReader; 7 import java.io.FileReader; 8 import java.io.IOException; 9 import java.text.DecimalFormat; 10 public class test { 11 public static void main(String[] args) throws IOException { 12 BufferedReader br = new BufferedReader(new FileReader("D:\\QQ documents\\JAVA课件/《飘》.txt")); 13 StringBuilder sb = new StringBuilder(); 14 while (true) { 15 String line = br.readLine(); 16 if (line == null) 17 break; 18 sb.append(line); 19 } 20 br.close(); 21 String content = sb.toString(); 22 int[] letter=new int[52]; 23 String M = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 24 char[] l = new char[52]; 25 l = M.toCharArray(); 26 for(int i=0;i<content.length();i++) { 27 char ch = content.charAt(i); 28 if(ch>='A'&&ch<='Z') { 29 letter[ch-65]++; 30 } 31 if(ch>='a'&&ch<='z') { 32 letter[ch-71]++; 33 } 34 } 35 int sum=content.length() - content.replaceAll("[a-zA-Z]", "").length(); 36 for(int i=0;i<51;i++) { 37 for(int j=0;j<51-i;j++) { 38 if(letter[j]<letter[j+1]) { 39 int temp1 = letter[j]; 40 letter[j]=letter[j+1]; 41 letter[j+1]=temp1; 42 char temp2 = l[j]; 43 l[j] = l[j+1]; 44 l[j+1] = temp2; 45 } 46 } 47 } 48 System.out.println("字母总数:"+sum); 49 System.out.println("各字母的频率如下:"); 50 for(int i=0;i<52;i++) { 51 double ending = (letter[i])*100/(double)sum; 52 // System.out.println(letter[i]); 53 System.out.println(l[i]+"出现的频率:"+new DecimalFormat("#.##").format(ending)+"%"); 54 } 55 // System.out.println("Character count is:" + (content.length() - content.replaceAll("[a-zA-Z]", "").length())); 56 // System.out.println("Number count is:" + (content.length() - content.replaceAll("[0-9]", "").length())); 57 // System.out.println("Special count is :" + content.replaceAll("[\\w ]", "").length()); 58 } 59 }
运行的结果如下:
标签:java,io,int,11.12,测试,import,new,课堂,String 来源: https://www.cnblogs.com/SHINIAN200/p/15548320.html