import java.io.; import java.util.; public class IdiomProcessor { public static void main(String[] args) { try { BufferedReader br = new BufferedReader(new FileReader("D:\yy\java\成语接龙\成语大全\去重后.txt")); List<String> idiomList = new ArrayList<>(); Map<Character, List<String>> idiomMap = new HashMap<>(); String line; while ((line = br.readLine()) != null) { String[] fields = line.split("拼音:|释义:|出处:|示例:"); if (fields.length == 5) { String idiom = fields[0].trim(); idiomList.add(idiom); char lastChar = idiom.charAt(idiom.length()-1); if (!idiomMap.containsKey(lastChar)) { idiomMap.put(lastChar, new ArrayList<>()); } idiomMap.get(lastChar).add(idiom); } } br.close(); BufferedWriter bw = new BufferedWriter(new FileWriter("D:\yy\java\成语接龙\成语大全\全量成语处理后.txt")); br = new BufferedReader(new FileReader("D:\yy\java\成语接龙\成语大全\去重后.txt")); while ((line = br.readLine()) != null) { String[] fields = line.split("拼音:|释义:|出处:|示例:"); if(fields.length==5) { String idiom = fields[0].trim(); char lastChar = idiom.charAt(idiom.length() - 1); int count=0; for(String nextIdiom:idiomMap.get(lastChar)) { if(nextIdiom.charAt(0) == lastChar) { count++; } } bw.write(line + "可接:" + count); bw.newLine(); } } bw.close(); br.close(); }catch (IOException e) { e.printStackTrace(); } } }修改这串代码,因为目前count无法正确统计可以接龙的成语数量
时间: 2024-03-11 11:49:03
浏览: 46
可以将count的初始值改为1,因为每个成语都可以接上自己,然后在循环中判断下一个成语是否以当前成语的最后一个字开头,如果是则将count加1。修改后的代码如下:
```java
import java.io.*;
import java.util.*;
public class IdiomProcessor {
public static void main([string](https://geek.csdn.net/edu/8802d631b97a4a6af1f4d0bbf8527465?dp_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NDQ0MDg2MiwiZXhwIjoxNzA3MzcxOTM4LCJpYXQiOjE3MDY3NjcxMzgsInVzZXJuYW1lIjoid2VpeGluXzY4NjQ1NjQ1In0.RrTYEnMNYPC7AQdoij4SBb0kKEgHoyvF-bZOG2eGQvc&spm=1055.2569.3001.10083)[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader("D:\yy\java\成语接龙\成语大全\去重后.txt"));
List<[string](https://geek.csdn.net/edu/8802d631b97a4a6af1f4d0bbf8527465?dp_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NDQ0MDg2MiwiZXhwIjoxNzA3MzcxOTM4LCJpYXQiOjE3MDY3NjcxMzgsInVzZXJuYW1lIjoid2VpeGluXzY4NjQ1NjQ1In0.RrTYEnMNYPC7AQdoij4SBb0kKEgHoyvF-bZOG2eGQvc&spm=1055.2569.3001.10083)> idiomList = new ArrayList<>();
Map<Character, List<[string](https://geek.csdn.net/edu/8802d631b97a4a6af1f4d0bbf8527465?dp_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NDQ0MDg2MiwiZXhwIjoxNzA3MzcxOTM4LCJpYXQiOjE3MDY3NjcxMzgsInVzZXJuYW1lIjoid2VpeGluXzY4NjQ1NjQ1In0.RrTYEnMNYPC7AQdoij4SBb0kKEgHoyvF-bZOG2eGQvc&spm=1055.2569.3001.10083)>> idiomMap = new HashMap<>();
[string](https://geek.csdn.net/edu/8802d631b97a4a6af1f4d0bbf8527465?dp_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NDQ0MDg2MiwiZXhwIjoxNzA3MzcxOTM4LCJpYXQiOjE3MDY3NjcxMzgsInVzZXJuYW1lIjoid2VpeGluXzY4NjQ1NjQ1In0.RrTYEnMNYPC7AQdoij4SBb0kKEgHoyvF-bZOG2eGQvc&spm=1055.2569.3001.10083) line;
while ((line = br.readLine()) != null) {
[string](https://geek.csdn.net/edu/8802d631b97a4a6af1f4d0bbf8527465?dp_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NDQ0MDg2MiwiZXhwIjoxNzA3MzcxOTM4LCJpYXQiOjE3MDY3NjcxMzgsInVzZXJuYW1lIjoid2VpeGluXzY4NjQ1NjQ1In0.RrTYEnMNYPC7AQdoij4SBb0kKEgHoyvF-bZOG2eGQvc&spm=1055.2569.3001.10083)[] fields = line.split("拼音:|释义:|出处:|示例:");
if (fields.length == 5) {
String idiom = fields[0].trim();
```