You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

36 lines
997 B

import java.util.ArrayList;
import java.util.List;
public class HistoryCommand {
private final List<String> commandHistory = new ArrayList<>();
public void recordCommand(String command) {
if (command != null && !command.isBlank()) {
commandHistory.add(command.trim());
}
}
public List<String> 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();
}
}