import java.util.ArrayList; import java.util.List; public class HistoryCommand { private final List commandHistory = new ArrayList<>(); public void recordCommand(String command) { if (command != null && !command.isBlank()) { commandHistory.add(command.trim()); } } public List getHistory() { return new ArrayList<>(commandHistory); } public void printHistory() { System.out.println("=== 命令历史 ==="); for (int i = 0; i < commandHistory.size(); i++) { System.out.printf("%d: %s%n", i + 1, commandHistory.get(i)); } } public void clearHistory() { commandHistory.clear(); } public static void main(String[] args) { HistoryCommand history = new HistoryCommand(); history.recordCommand("crawl `https://example.com` "); history.recordCommand("list"); history.recordCommand("exit"); history.printHistory(); } }