java问题:统计用户输入的一段文字的字符个数、单词数、和句子个数!
2个回答
展开全部
import java.util.HashMap;
import java.util.Map;
import java.io.*;
public class test{
private static String WORD = "0";
private static String SENTENCE = "1";
public static Map<String, Integer> getCount(String str) {
str = str.replace(".", "#");
str = str.replace("?", "#");
str = str.replace("!", "#");
int wordCount = str.split(" ").length;
int sentenceCount = str.split("#").length;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put(WORD, wordCount);
map.put(SENTENCE, sentenceCount);
return map;
}
public static void main(String[] args) throws NumberFormatException, IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String x=br.readLine();
Map<String, Integer> map = getCount(x);
System.out.println("单词数量: "+map.get(WORD));
System.out.println("句子数量: " + map.get(SENTENCE));
}
}
import java.util.Map;
import java.io.*;
public class test{
private static String WORD = "0";
private static String SENTENCE = "1";
public static Map<String, Integer> getCount(String str) {
str = str.replace(".", "#");
str = str.replace("?", "#");
str = str.replace("!", "#");
int wordCount = str.split(" ").length;
int sentenceCount = str.split("#").length;
Map<String, Integer> map = new HashMap<String, Integer>();
map.put(WORD, wordCount);
map.put(SENTENCE, sentenceCount);
return map;
}
public static void main(String[] args) throws NumberFormatException, IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String x=br.readLine();
Map<String, Integer> map = getCount(x);
System.out.println("单词数量: "+map.get(WORD));
System.out.println("句子数量: " + map.get(SENTENCE));
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询