Java代码实现输入一段代码,当输入exit停止,统计这段代码中的各个关键词个数,需要再创建一个方法除去代码中的注释部分,输出结果按照关键字升序输出
时间: 2024-02-12 14:08:33 浏览: 16
好的,下面是 Java 代码实现:
```java
import java.util.*;
public class KeywordCounter {
// 定义 Java 关键词数组
private static final String[] KEYWORDS = {"abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "enum", "extends", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native", "new", "package", "private", "protected", "public", "return", "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "try", "void", "volatile", "while"};
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StringBuilder codeBuilder = new StringBuilder();
String line;
// 读入代码,直到输入 exit 停止
while (true) {
line = scanner.nextLine();
if (line.equals("exit")) {
break;
}
codeBuilder.append(line).append('\n');
}
String code = codeBuilder.toString();
// 统计关键词个数
Map<String, Integer> keywordCountMap = countKeywords(code);
// 输出关键词个数
System.out.println("关键词个数统计:");
for (Map.Entry<String, Integer> entry : keywordCountMap.entrySet()) {
System.out.printf("%s: %d\n", entry.getKey(), entry.getValue());
}
}
// 统计代码中各个关键词个数
private static Map<String, Integer> countKeywords(String code) {
Map<String, Integer> keywordCountMap = new TreeMap<>();
// 将代码按空格和换行符分割成单词
String[] words = code.split("\\s+|\n");
// 遍历单词,如果是关键词则计数器加 1
for (String word : words) {
if (Arrays.asList(KEYWORDS).contains(word)) {
keywordCountMap.put(word, keywordCountMap.getOrDefault(word, 0) + 1);
}
}
return keywordCountMap;
}
}
```
上面的代码实现了输入一段代码并统计其关键词个数,当输入 exit 停止输入。
下面是要求的创建一个方法除去代码中的注释部分,并按照关键字升序输出结果的代码:
```java
import java.util.*;
public class KeywordCounter {
// 定义 Java 关键词数组
private static final String[] KEYWORDS = {"abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "enum", "extends", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native", "new", "package", "private", "protected", "public", "return", "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "try", "void", "volatile", "while"};
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StringBuilder codeBuilder = new StringBuilder();
String line;
// 读入代码,直到输入 exit 停止
while (true) {
line = scanner.nextLine();
if (line.equals("exit")) {
break;
}
codeBuilder.append(line).append('\n');
}
String code = codeBuilder.toString();
// 去除注释
code = removeComments(code);
// 统计关键词个数
Map<String, Integer> keywordCountMap = countKeywords(code);
// 输出关键词个数
System.out.println("关键词个数统计:");
for (Map.Entry<String, Integer> entry : keywordCountMap.entrySet()) {
System.out.printf("%s: %d\n", entry.getKey(), entry.getValue());
}
}
// 统计代码中各个关键词个数
private static Map<String, Integer> countKeywords(String code) {
Map<String, Integer> keywordCountMap = new TreeMap<>();
// 将代码按空格和换行符分割成单词
String[] words = code.split("\\s+|\n");
// 遍历单词,如果是关键词则计数器加 1
for (String word : words) {
if (Arrays.asList(KEYWORDS).contains(word)) {
keywordCountMap.put(word, keywordCountMap.getOrDefault(word, 0) + 1);
}
}
return keywordCountMap;
}
// 去除代码中的注释
private static String removeComments(String code) {
StringBuilder codeBuilder = new StringBuilder();
boolean inSingleLineComment = false; // 是否在单行注释中
boolean inMultiLineComment = false; // 是否在多行注释中
for (int i = 0; i < code.length(); i++) {
char c = code.charAt(i);
if (inSingleLineComment) {
if (c == '\n') {
inSingleLineComment = false;
codeBuilder.append(c);
}
} else if (inMultiLineComment) {
if (c == '*' && i < code.length() - 1 && code.charAt(i + 1) == '/') {
inMultiLineComment = false;
i++;
}
} else {
if (c == '/') {
if (i < code.length() - 1) {
char nextChar = code.charAt(i + 1);
if (nextChar == '/') {
inSingleLineComment = true;
i++;
} else if (nextChar == '*') {
inMultiLineComment = true;
i++;
} else {
codeBuilder.append(c);
}
} else {
codeBuilder.append(c);
}
} else {
codeBuilder.append(c);
}
}
}
return codeBuilder.toString();
}
}
```
这个版本在原有的基础上添加了 `removeComments` 方法,用于去除代码中的注释。去除注释后再统计关键词个数,并按照关键字升序输出结果。