java判断一句话中的字母个数
要用Java实现这么一个程序:输入一句英文:Nowisthetimeforallgoodmentocometotheaidoftheircountry这句话有53个字母....
要用Java实现这么一个程序:
输入一句英文:
Now is the time for all good men to come to the aid of their country
这句话有 53 个字母.
这句话有 16 个单词.
最长的一个单词有 7 个字母.
每个字母出现的频率是
A -- 2
C -- 2
D -- 2
E -- 6
F -- 2
G -- 1
H -- 3
I -- 4
L -- 2
M -- 3
N -- 2
O -- 9
R -- 3
S -- 1
T -- 7
U -- 1
W -- 1
Y -- 1
如何来判断每个字母出现的次数呢~~
希望有高人指导下!谢谢!! 展开
输入一句英文:
Now is the time for all good men to come to the aid of their country
这句话有 53 个字母.
这句话有 16 个单词.
最长的一个单词有 7 个字母.
每个字母出现的频率是
A -- 2
C -- 2
D -- 2
E -- 6
F -- 2
G -- 1
H -- 3
I -- 4
L -- 2
M -- 3
N -- 2
O -- 9
R -- 3
S -- 1
T -- 7
U -- 1
W -- 1
Y -- 1
如何来判断每个字母出现的次数呢~~
希望有高人指导下!谢谢!! 展开
5个回答
展开全部
public class Test {
public HashMap<String, Integer> getcount(String content) {
String chars = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
String[] char_arry_tolower = chars.split(",");
List<String> list = new ArrayList<String>();
for (String char_array : char_arry_tolower) {
list.add(char_array); //小写
list.add(char_array.toUpperCase()); //变成大写
}
HashMap<String, Integer> hash = new HashMap<String, Integer>();
for (int i=0;i<content.length();i++) {
char ch = content.charAt(i);
for (String str : list) {
if (String.valueOf(ch).equals(str)) {
if (hash.get(str) != null) {
int count = hash.get(str);
count++;
hash.put(str, count);
} else {
hash.put(str, 1);
}
}
}
}
return hash;
}
public static void main(String[] args) throws Exception{
Test test = new Test();
String content = "Now is the time for all good men to come to the aid of their country";
HashMap<String,Integer> hash = test.getcount(content);
Set<String> set = hash.keySet();
for (String string : set) {
System.out.println(string+"===>"+hash.get(string));
}
}
}
刚写的直接用,content里面输入自己想测试的内容,希望给我追加点分,写的很辛苦。。
public HashMap<String, Integer> getcount(String content) {
String chars = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
String[] char_arry_tolower = chars.split(",");
List<String> list = new ArrayList<String>();
for (String char_array : char_arry_tolower) {
list.add(char_array); //小写
list.add(char_array.toUpperCase()); //变成大写
}
HashMap<String, Integer> hash = new HashMap<String, Integer>();
for (int i=0;i<content.length();i++) {
char ch = content.charAt(i);
for (String str : list) {
if (String.valueOf(ch).equals(str)) {
if (hash.get(str) != null) {
int count = hash.get(str);
count++;
hash.put(str, count);
} else {
hash.put(str, 1);
}
}
}
}
return hash;
}
public static void main(String[] args) throws Exception{
Test test = new Test();
String content = "Now is the time for all good men to come to the aid of their country";
HashMap<String,Integer> hash = test.getcount(content);
Set<String> set = hash.keySet();
for (String string : set) {
System.out.println(string+"===>"+hash.get(string));
}
}
}
刚写的直接用,content里面输入自己想测试的内容,希望给我追加点分,写的很辛苦。。
展开全部
//前面的我想你应该会吧
import java.util.Map;
import java.util.TreeMap;
public class CharNum {
private static Map<String, Integer> map = new TreeMap<String, Integer>();
static {
for(char c = 'A'; c <= 'Z'; c ++) {
map.put(c + "", new Integer(0));
}
}
public CharNum() {
}
public static void print(String line) {
char[] cs = line.toCharArray();
for(char c: cs) {
if(Character.isLetter(c)) {
c = Character.toUpperCase(c);
String key = c + "";
Integer time = map.get(key);
map.put(key, Integer.valueOf(time.intValue() + 1));
}
}
for(char c = 'A'; c <= 'Z'; c ++) {
String key = c + "";
Integer in = map.get(key);
if(in.intValue() != 0) {
System.out.println(c + " -- " + in.intValue());
}
}
}
public static void main(String[] args) {
String line = "Now is the time for all good men to come to the aid of their country";
CharNum.print(line);
}
}
import java.util.Map;
import java.util.TreeMap;
public class CharNum {
private static Map<String, Integer> map = new TreeMap<String, Integer>();
static {
for(char c = 'A'; c <= 'Z'; c ++) {
map.put(c + "", new Integer(0));
}
}
public CharNum() {
}
public static void print(String line) {
char[] cs = line.toCharArray();
for(char c: cs) {
if(Character.isLetter(c)) {
c = Character.toUpperCase(c);
String key = c + "";
Integer time = map.get(key);
map.put(key, Integer.valueOf(time.intValue() + 1));
}
}
for(char c = 'A'; c <= 'Z'; c ++) {
String key = c + "";
Integer in = map.get(key);
if(in.intValue() != 0) {
System.out.println(c + " -- " + in.intValue());
}
}
}
public static void main(String[] args) {
String line = "Now is the time for all good men to come to the aid of their country";
CharNum.print(line);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用map吧,用出现的字母做为key,出现的次数做value
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public static int getCount(String s,char c){ //s是字符串,c是要查找的字母
int count = 0;
for(int i=0;i<s.length();i++){
if(s.charAt(i) == c){
count++;
}
}
return count;
}
int count = 0;
for(int i=0;i<s.length();i++){
if(s.charAt(i) == c){
count++;
}
}
return count;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
设一个长度为26的一维数组存就可以了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询