问题1:
输出结果要类似这样的:
Sample run of the program (user input is bold)
Please enter a Sentence
The Quick Brown Fox
Abbreviated String: Th Qck Brwn Fx
Sentence Stats
Number of Words: 4
Average number of letters dropped per word: 1.25
Do you want to try another? (y/n)
Y
Please enter a Sentence
I like bananas
Abbreviated String: I lk bns
Sentence Stats
Number of Words: 3
Average number of letters dropped per word: 2.0
Do you want to try another? (y/n)
y
Please enter a Sentence
Laughing should never be banned
Abbreviated String: Lghng shld nvr b bnd
Sentence Stats
Number of Words: 5
Average number of letters dropped per word: 2.2
Do you want to try another? (y/n)
n
public class sele {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "fhjdhgj fdsgj fd";
StringBuffer s1 = new StringBuffer(s);
//System.out.print(s);
for(int i =0;i<s1.length();i++){
char c = s1.charAt(i);
if (c == 'h'){
s1.deleteCharAt(i);
}
}
System.out.print(s1);
}
}
能实现把字符里的特殊字符去掉,关于计数的问题和输入的问题就得靠你自己解决了,毕竟自学也是一个考验自己的能力
计数只要在循环里加入个int变量就可以,输入就得String args [ ] 实现了
import java.io.*;
public class Demo5 {
private static BufferedReader br;
private static int count = 0;
static {
br = new BufferedReader(new InputStreamReader(System.in));
}
public static void main(String[] args) {
System.out.println("Please enter a Sentence");
String str = null;
StringBuffer result = new StringBuffer();
try {
str = br.readLine();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
String[] strs = str.split(" ");
for(String strValue:strs) {
String st = handler(strValue);
result.append(handler2(st).toCharArray()).append(" ");
}
System.out.print("Abbreviated String:");
System.out.println(result.toString());
System.out.print("Number of Words: ");
int num = strs.length;
System.out.println(num);
System.out.print("Average number of letters dropped per word:");
System.out.println((float)count/num);
}
//剔除元音字母
private static String handler(String str) {
if(str.length()==1)
return str;
StringBuffer sb = new StringBuffer(str);
int i = 0;
while(i<sb.length()) {
char ch = sb.charAt(i);
if((ch=='a')||(ch=='e')||(ch=='i')||(ch=='u')||(ch=='O')||(ch=='A')||(ch=='E')||(ch=='I')||(ch=='U')||(ch=='o')) {
sb = sb.deleteCharAt(i);
count++;
}else{
i++;
}
}
return sb.toString();
}
//剔除连着的辅音
private static String handler2(String str) {
StringBuffer sb = new StringBuffer(str);
int i = 0;
while(i<sb.length()-1) {
int j=i+1;
if((sb.charAt(i))==(sb.charAt(j))) {
sb.deleteCharAt(j);
count++;
}else{
i++;
}
}
return sb.toString();
}
}