这样就行:
import java.io.*;
public class Demon
{
public static void main(String args[])
{
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str="";
String sen="";//英文句子
String[] word;
int count;
double percent;
while(str.matches("[nN]")==false)
{
System.out.println("Please enter a Sentence");
sen=br.readLine();
count=sen.length();//记录句子的原长
word=sen.split("\\s");//分割句子为单词数组
sen="";
for(String temp : word) //去除元音字母和重复字符
{
temp=temp.replaceAll("[aeiou]","");
temp=temp.replaceAll("(?s)(.)(?=.?\\1)", "");
sen+=temp+" ";
}
percent=(double)(count-sen.length()+1)/(double)word.length;//计算平均每个单词删除的字数
System.out.println("Abbreviated String:"+sen);
System.out.println("Sentence Stats");
System.out.println("Number of Words:"+word.length);
System.out.println("Average number of letters dropped per word:"+percent);
System.out.println("Do you want to try another? (y/n)");
str = br.readLine();
}
}
catch(IOException ex)
{
System.out.println("发生异常:"+ex.getMessage());
}
}
}
温馨提示:内容为网友见解,仅供参考