用JAVA程序,编程一个英文段落中各字母(不分大小写)出现的次数并输出统计结果。(用检索方法)
发布网友
发布时间:2022-05-26 07:05
我来回答
共1个回答
热心网友
时间:2023-10-05 10:41
import java.util.*;
public class CharNumber {
public void test()
{
Scanner scan=new Scanner(System.in);
System.out.println("input the string:");
String s=scan.nextLine();
char[] ch=s.toCharArray();
double[] percent=new double[ch.length];//存放比例
char[] resultch=new char[ch.length];
int[] resultnum=new int[ch.length];
int index=0;
for(int i=0;i<ch.length;i++)
{
if(i==0) {
resultch[index]=ch[i];
resultnum[index]++;
}
else
{
int j;
for(j=0;j<=index;j++)
{
if(ch[i]!=resultch[j]) {
continue;
}
else {
resultnum[j]++;break;
}
}
if(j>index) {
index++;
resultch[index]=ch[i];
resultnum[index]++;
}
}
}
for(int x=0;x<=index;x++)
{
percent[x]=Math.round(((double)resultnum[x]/ch.length)*100)/100.0;
}
//排序,插入排序
for(int ii=1;ii<=index;ii++)
{
int pos=ii;
int prepos=pos-1;
double temp=percent[pos];
double pre=percent[prepos];
char tempch=resultch[pos];
while(prepos>=0)
{
if(temp<pre)
{
percent[prepos+1]=percent[prepos];
resultch[prepos+1]=resultch[prepos];
prepos--;
}
else {
percent[prepos+1]=temp;
resultch[prepos+1]=tempch;
break;
}
}
if(prepos<0){
percent[prepos+1]=temp;
resultch[prepos+1]=tempch;
}
}
//输出
for(int i=0;i<=index;i++)
{
System.out.println(resultch[i]+":"+percent[i]);
}
}
}追问我有点看不懂,有没有简单点的方法?这是一个方法这么长?最好用:public static void main(String[] args)