Мне нужна помощь в создании меню с параметрами, которые выбирают другие меню, не вызывая Stackoverflow из -за рекурсии.
Но меню должно быть инициализировано, и меню A может указывать на меню B и B на A. Также важно, чтобы некоторые меню могли иметь различное количество вариантов. < /P>
public class Menus {
public static Menu mainMenu = new Menu("Main Menu");
public static Menu homeMenu = new Menu("Home Menu");
public static Menu BMenu = new Menu("B Menu");
public static void initMenus() {
mainMenu.addOptions(
new MenuOption(
"bmenu",
BMenu
),
new MenuOption(
"exit",
mainMenu,
CLI::stop
)
);
BMenu.addOptions(
new MenuOption(
"main",
mainMenu
),
new MenuOption(
"exit",
BMenu,
CLI::stop
)
);
}
}
< /code>
public class MenuOption {
private final String name;
private final Runnable[] actions;
private final Menu nextMenu;
public MenuOption(String name, Menu nextMenu, Runnable... actions) {
this.name = name;
this.actions = actions;
this.nextMenu = nextMenu;
}
public MenuOption(String name, Menu nextMenu) {
this.name = name;
this.actions = new Runnable[0];
this.nextMenu = nextMenu;
}
public void run() {
for (Runnable r : actions) {
r.run();
}
}
public void run(int delay) {
for (Runnable r : actions) {
r.run();
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
}
public String getName() {
return name;
}
public Menu getNextMenu() {
return nextMenu;
}
}
< /code>
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Menu {
private final String title;
private final List options;
public Menu(String title, List options) {
this.title = title;
this.options = options;
}
public Menu(String title, MenuOption... options) {
this.title = title;
this.options = new ArrayList(Arrays.asList(options));
}
public String getTitle() {
return title;
}
public List getOptions() {
return options;
}
public Menu select(int index) {
if (index > 0 && index
import java.util.Arrays;
import java.util.Scanner;
public class CLI {
private static Menu currentState = Menus.mainMenu;
private static volatile boolean running = true;
public static void run() {
Scanner scanner = new Scanner(System.in);
Menus.initMenus();
while (running) {
clearAndDisplay();
currentState = currentState.select(scanner.nextInt());
}
}
public static String generateMenu(String Title, String... options ) {
StringBuilder menu = new StringBuilder();
menu.append(Title).append("\n");
for (int option = 1; option
currently they are assigned to values if they don't exist, and if they do, return the value. But first menu can't be created, because the other one is null, and the second one can't be created, because the first one is null. I was thinking I could maybe use a placeholder, leaving the problematic option empty, creating the menus and then assigning the option, but I'm not sure how I would go about it. Can anyone help? Is there a much easier way to create this concept that I'm not seeing?
I tried to figure out a way to get both menus initialized, but I can't figure this out
EDIT: managed to fix the problem, but I'm not confident that this implementation is done well, please lmk if you see an issue
Подробнее здесь: https://stackoverflow.com/questions/796 ... ut-causing
Мне нужна помощь в создании меню с опциями, которые выбирают другие меню, не вызывая Stackoverflow из -за рекурсии ⇐ JAVA
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Почему я получаю ошибку рекурсии, если глубина ожидаемой рекурсии должна быть меньше 999?
Anonymous » » в форуме Python - 0 Ответы
- 33 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Почему я получаю ошибку рекурсии, если глубина ожидаемой рекурсии должна быть меньше 999?
Anonymous » » в форуме Python - 0 Ответы
- 37 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Почему я получаю ошибку рекурсии, если глубина ожидаемой рекурсии должна быть меньше 999?
Anonymous » » в форуме Python - 0 Ответы
- 34 Просмотры
-
Последнее сообщение Anonymous
-