import java.math.BigDecimal;
import java.util.*;
public class Text3_25
{
static String str = "";
public static void sum(int n)//用来储存输入的字符串
{
Scanner input = new Scanner(System.in);
for(int i = 0 ; i < n ;i ++)
{
str += input.next();
}
max(str);//调用max方法
}
public static void max(String str)
{
int cont = 0;
for (int i = 0; i < str.length(); i++)//统计字符的个数
{
if(str.charAt(i)>='a'&&str.charAt(i)<='z'||str.charAt(i)>='A'&&str.charAt(i)<='Z')
{
cont ++;
}
}
System.out.println(cont);
int a[] = new int [26];
for (int i = 0; i < str.length(); i++)//统计每个单词出现的个数
{
char x = str.charAt(i);
if(x >='A' && x <= 'Z')//出现大写字么,转换为小写字母
{
x = (char) (x+32) ;
}
switch (x) {
case 'a':a[0]++;
break;
case 'b':a[1]++;
break;
case 'c':a[2]++;
break;
case 'd':a[3]++;
break;
case 'e':a[4]++;
break;
case 'f':a[5]++;
break;
case 'g':a[6]++;
break;
case 'h':a[7]++;
break;
case 'i':a[8]++;
break;
case 'j':a[9]++;
break;
case 'k':a[10]++;
break;
case 'l':a[11]++;
break;
case 'm':a[12]++;
break;
case 'n':a[13]++;
break;
case 'o':a[14]++;
break;
case 'p':a[15]++;
break;
case 'q':a[16]++;
break;
case 'r':a[17]++;
break;
case 's':a[18]++;
break;
case 't':a[19]++;
break;
case 'u':a[20]++;
break;
case 'v':a[21]++;
break;
case 'w':a[22]++;
break;
case 'x':a[23]++;
break;
case 'y':a[24]++;
break;
case 'z':a[25]++;
break;
default:
break;
}
}
for(int i = 0 ; i<a.length ; i++)//计算每个字母出现的百分比并打印
{
double x = (double)a[i]/cont;
double f = T(x);
if(x == 0)
{
continue;
}
System.out.println((char)('a'+i)+":"+f+"%");
}
}
public static double T(double x)//实现四舍五入,保留二位小数
{
double f = x;
BigDecimal b =new BigDecimal(f);
double f1 = b.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
return f1;
}
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println(" 输入的行数");
int N = input.nextInt();//从键盘输入n
sum(N);
}
}
有没有什么更简单的方法可以用? |
|