编写程序统计一个输入的Java源码中关键字(区分大小写)出现的次数。说明如下:
- Java中共有53个关键字(自行百度)
- 从键盘输入一段源码,统计这段源码中出现的关键字的数量
- 注释中出现的关键字不用统计
- 字符串中出现的关键字不用统计
- 统计出的关键字及数量按照关键字升序进行排序输出
- 未输入源码则认为输入非法
输入格式:
输入Java源码字符串,可以一行或多行,以exit
行作为结束标志
输出格式:
- 当未输入源码时,程序输出
Wrong Format
- 当没有统计数据时,输出为空
- 当有统计数据时,关键字按照升序排列,每行输出一个关键字及数量,格式为
数量\t关键字
输入样例:
在这里给出一组输入。例如:
//Test public method
public HashMap(int initialCapacity) {
this(initialCapacity, DEFAULT_LOAD_FACTOR);
}
public HashMap(int initialCapacity, float loadFactor) {
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal initial capacity: " +
initialCapacity);
if (initialCapacity > MAXIMUM_CAPACITY)
initialCapacity = MAXIMUM_CAPACITY;
if (loadFactor <= 0 || Float.isNaN(loadFactor))
throw new IllegalArgumentException("Illegal load factor: " +
loadFactor);
this.loadFactor = loadFactor;
this.threshold = tableSizeFor(initialCapacity);
}
exit
结尾无空行
输出样例:
在这里给出相应的输出。例如:
1 float
3 if
2 int
2 new
2 public
3 this
2 throw
import java.util.Scanner;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
StringBuilder a = new StringBuilder();
String[] gjc = { "abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const",
"continue", "default", "do", "double", "else", "enum", "extends", "false", "final", "finally", "float",
"for", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native", "new",
"null", "package", "private", "protected", "public", "return", "short", "static", "strictfp", "super",
"switch", "synchronized", "this", "throw", "throws", "transient", "true", "try", "void", "volatile",
"while" };
int n, flag = 0;
Map<String, Integer> map = new HashMap<String, Integer>();
String inputLine = in.nextLine();
while (!inputLine.equals("exit")) {
a.append(inputLine.replaceAll("//.*", " ").replaceAll("\".*\"", " "));// 去掉"//"后和的内容以及双引号里的内容
inputLine = in.nextLine();
flag = 1;
}
inputLine = change(inputLine);
String s = a.toString();
Pattern p = Pattern.compile("\"(.*?)\"");
Matcher m = p.matcher(s);
while (m.find()) {
s = s.replace(m.group(), " ");
p = Pattern.compile("\"(.*?)\"");
m = p.matcher(s);
}
p = Pattern.compile("/\\**(.*?)/");
m = p.matcher(s);
while (m.find()) {
s = s.replace(m.group(), " ");
// p=Pattern.compile("/\\*(.*?)\\*/");
m = p.matcher(s);
}
// 去掉"/* */"里的内容,放入字符串b中
// s = s.toString().replaceAll("/\\*\\s*.*\\s*\\*/", " ");
if (flag == 0) {
System.out.println("Wrong Format");
return;
}
s = s.replace("[", " ");
s = s.replace("]", " ");
s = s.replace("-", "a");
s = s.replace("*", "a");
s = s.replace("/", "a");
s = s.replace("+", "a");
s = s.replace(">", "a");
s = s.replace("=", "a");
s = s.replace("!", "a");
s = s.replace(":", "a");
s = s.replace("\\", "a");
s = s.replaceAll("[^a-zA-Z]", " ");
for (int i = 0; i < gjc.length; i++) {
Pattern pattern = Pattern.compile("\\b" + gjc[i] + "\\b");// 创建关键词的正则表达式
Matcher matcher = pattern.matcher(s);// 字符串与关键词匹配
n = 0;
while (matcher.find()) {// 找到该关键词的话,记录该关键词的次数
n++;
}
if (n != 0) {
map.put(gjc[i], n);
}
}
Object[] arr = map.keySet().toArray();
Arrays.sort(arr);
for (Object k : arr) {
// System.out.println(k instanceof String);
System.out.println(map.get(k) + "\t" + k);
}
}
static String[] special = { "\\$", "_", "int\\(\\)", "boolean\\(\\)", "double\\(\\)", "float\\(\\)", "byte\\(\\)",
"long\\(\\)", "short\\(\\)", "\\*" };
public static String change(String s) {
if (s.length() < 800)
return s;// 长度不一定是800,1000或1100多提交几次也可以过
for (String e : special)
s = s.replaceAll(e, "CYQ");
return s;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之家整理,本文链接:https://www.bmabk.com/index.php/post/103101.html