temp

问题1:

大概是输入一段英文句子 删除单词里面的元音字母像like就删除i,e。 如果单词只有一个字母像a 就不用删除,将两个连续相同的非原音字母去除一个,将结果输出,并计算单词数和平均每个单词删除的字数

输出结果要类似这样的:
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 [ ] 实现了
-----------------------------------------------------------------------------------------
package com.topsun.test;

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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值