You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java作业技术求助:完善查看操作历史模块与循环输入逻辑

解决Java编码解码程序的两个需求:操作历史记录与持续运行逻辑

嘿,针对你的Java作业需求,我整理了一套完整的解决方案,咱们一步步来实现:

核心需求拆解

  1. 让程序持续接收用户输入,直到选择退出;
  2. 用动态存储结构(比固定数组更灵活)记录所有操作,并按指定格式展示。

修改后的完整代码(带详细注释)

import java.util.ArrayList;
import java.util.Scanner;

public class CodecProgram {
    // 假设你已经实现了这些编解码器类,这里先做实例化
    private static CaesarEncoder caesarEncoder = new CaesarEncoder();
    private static CaesarDecoder caesarDecoder = new CaesarDecoder();
    private static MorseEncoder morseEncoder = new MorseEncoder();
    private static MorseDecoder morseDecoder = new MorseDecoder();

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 用ArrayList存储操作历史,不用提前指定长度,动态添加更方便
        ArrayList<String> operationHistory = new ArrayList<>();
        int operationSeq = 1; // 记录操作的序号

        // do-while循环实现持续运行,直到用户主动退出
        do {
            // 打印操作菜单
            System.out.println("\nPlease select operation");
            System.out.println("1.Encode");
            System.out.println("2.Decode");
            System.out.println("3.View operation history");
            System.out.println("4.Exit program");

            Integer userChoice;
            // 处理用户输入非数字的异常,避免程序直接崩溃
            try {
                userChoice = Integer.valueOf(in.nextLine());
            } catch (NumberFormatException e) {
                System.out.println("Invalid input! Please enter a number between 1-4.");
                continue; // 回到菜单重新选择
            }

            if (userChoice.equals(1)) {
                System.out.println("You chosen encoding operation");
                System.out.println("Please choose codec name - caesar or morse");
                String codec = in.nextLine().toLowerCase();
                if (codec.equals("caesar")) {
                    System.out.println("Enter text to encode");
                    String inputText = in.nextLine().toLowerCase();
                    String result = caesarEncoder.encode(inputText);
                    System.out.println("Encoded text: " + result);
                    // 按要求格式添加操作记录
                    operationHistory.add(String.format("[%d]. Encode - %s", operationSeq++, inputText));
                } else if (codec.equals("morse")) {
                    System.out.println("Enter text to encode");
                    String inputText = in.nextLine().toLowerCase();
                    String result = morseEncoder.encode(inputText);
                    System.out.println("Encoded text: " + result);
                    operationHistory.add(String.format("[%d]. Encode - %s", operationSeq++, inputText));
                } else {
                    System.out.println("You entered wrong codec name, try one more time");
                    // 错误操作也可以记录到历史里,方便排查
                    operationHistory.add(String.format("[%d]. Encode - Invalid codec: %s", operationSeq++, codec));
                }
            } else if (userChoice.equals(2)) {
                System.out.println("You chosen decoding operation");
                System.out.println("Please choose codec name - caesar or morse");
                String codec = in.nextLine().toLowerCase();
                if (codec.equals("caesar")) {
                    System.out.println("Enter text to decode");
                    String inputText = in.nextLine().toLowerCase();
                    String result = caesarDecoder.decode(inputText);
                    System.out.println("Decoded text: " + result);
                    operationHistory.add(String.format("[%d]. Decode - %s", operationSeq++, inputText));
                } else if (codec.equals("morse")) {
                    System.out.println("Enter text to Decode");
                    String inputText = in.nextLine().toLowerCase();
                    String result = morseDecoder.decode(inputText);
                    System.out.println("Decoded text: " + result);
                    operationHistory.add(String.format("[%d]. Decode - %s", operationSeq++, inputText));
                } else {
                    System.out.println("You entered wrong codec name, try one more time");
                    operationHistory.add(String.format("[%d]. Decode - Invalid codec: %s", operationSeq++, codec));
                }
            } else if (userChoice.equals(3)) {
                System.out.println("=== Operation History ===");
                if (operationHistory.isEmpty()) {
                    System.out.println("No operations recorded yet.");
                } else {
                    // 遍历打印所有历史记录
                    for (String record : operationHistory) {
                        System.out.println(record);
                    }
                }
            } else if (userChoice.equals(4)) {
                in.close();
                System.out.println("Program was closed by user");
                break; // 退出循环,结束程序
            } else {
                System.out.println("Invalid option! Please select a number between 1-4.");
                operationHistory.add(String.format("[%d]. Invalid operation: %d", operationSeq++, userChoice));
            }
        } while (true); // 无限循环直到用户选择退出
    }
}

关键修改说明

  • 持续运行逻辑:用do-while(true)把整个菜单和操作流程包裹起来,只有用户选择4时,通过break退出循环,结束程序;
  • 操作历史存储:选用ArrayList<String>替代固定数组,支持动态添加记录,不用操心数组长度不够的问题;
  • 异常处理:添加了try-catch捕获用户输入非数字的情况,避免程序直接崩溃;
  • 历史展示:选择3时遍历历史列表打印,空列表时给出友好提示。

这样修改后,你的程序就能完美满足两个需求啦,用户可以反复执行操作、随时查看历史,直到主动退出。

内容的提问来源于stack exchange,提问作者Daeris

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:13:29