2301_79988164 2024-05-28 20:49 采纳率: 33.3%
浏览 1

关于该java代码求解决

遇到一个难题,不知道怎么解决,还请教各位网友给出一个解决方案。自己已经试了好几次都是测试不通过。

img

img

img

img

![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/c53cb4c051e44da8863d3e78

img


90231b6f.jpeg "#left")

  • 写回答

2条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2024-05-28 20:49
    关注

    引自免费微信小程序:皆我百晓生

    从您提供的代码片段来看,似乎缺少了一些关键部分,特别是CommandMenuMenuItem类的实现。不过,我可以基于您的描述和给出的代码片段,给出一个简化版的实现。为了方便理解,我会逐步解释各个部分。

    首先,我们需要定义一个菜单项MenuItem,它包含一个标识符和一个描述。当用户选择这个菜单项时,会触发一个回调方法。

    这里是一个简单的MenuItem实现:

    import java.util.function.Consumer;
    
    public class MenuItem {
        private char key;
        private String description;
        private Consumer<MenuItem> listener;
    
        public MenuItem(String description, char key) {
            this.description = description;
            this.key = key;
        }
    
        public void setOnSelectListener(Consumer<MenuItem> listener) {
            this.listener = listener;
        }
    
        public char getKey() {
            return key;
        }
    
        public String getDescription() {
            return description;
        }
    
        public void execute() {
            if (listener != null) {
                listener.accept(this);
            }
        }
    }
    

    接下来,我们创建一个CommandMenu类,它负责管理菜单项、显示菜单以及处理用户输入。

    这是一个简单的CommandMenu实现:

    import java.util.Scanner;
    
    public class CommandMenu {
        private final String title;
        private final MenuItem exitItem;
        private final MenuItem[] items;
        private int currentIndex;
    
        public CommandMenu(String title) {
            this.title = title;
            this.exitItem = new MenuItem("Exit Menu(X)", 'X');
            this.items = new MenuItem[2];
            this.currentIndex = 0;
        }
    
        public void addItem(MenuItem item) {
            if (currentIndex < items.length) {
                items[currentIndex++] = item;
            } else {
                throw new IllegalStateException("Maximum number of menu items reached.");
            }
        }
    
        public void show() {
            System.out.println(title);
            for (int i = 0; i < currentIndex; i++) {
                System.out.printf("[%d]. %s (%c)%n", i + 1, items[i].getDescription(), items[i].getKey());
            }
            System.out.printf("[%d]. %s (%c)%n", currentIndex + 1, exitItem.getDescription(), exitItem.getKey());
    
            Scanner scanner = new Scanner(System.in);
            while (true) {
                System.out.print("Input Command ~ % ");
                String input = scanner.nextLine();
                char commandKey = input.charAt(0);
    
                if (commandKey == exitItem.getKey()) {
                    break;
                } else {
                    for (int i = 0; i <= currentIndex; i++) {
                        if (items[i] != null && items[i].getKey() == commandKey) {
                            items[i].execute();
                            return;
                        }
                    }
                    System.out.println("Invalid Menu Key '" + commandKey + "'!");
                }
            }
        }
    }
    

    然后,我们可以使用这些类来创建一个简单的菜单并运行它:

    public class Main {
        public static void main(String[] args) {
            CommandMenu mainMenu = new CommandMenu("Welcome to CommandLineMenu Demo");
            MenuItem firstItem = new MenuItem("The First Menu", 'F');
            firstItem.setOnSelectListener(item -> System.out.println("The First Menu is selected!"));
            mainMenu.addItem(firstItem);
    
            MenuItem secondItem = new MenuItem("The Second Menu", 'S');
            secondItem.setOnSelectListener(item -> System.out.println("The Second Menu is selected!"));
            mainMenu.addItem(secondItem);
    
            mainMenu.show();
        }
    }
    

    这段代码应该能实现您提供的例子中的基本功能。您可以根据需求进行扩展,比如添加更多的菜单项,或者调整界面样式等。如果在实际操作中遇到问题,请随时提问。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 5月28日