我有两个班级:
主类-做两件事: 1)接收一个输入作为参数,然后使用该输入从开关中选择一个“ case” 2)接收输入作为参数,然后将该输入用于其他方法
GUI类: 包含控制台Scannner,读取用户输入并将用户输入传递给Main类。
问题在于,我无法获取进入子菜单级别的代码!例如,我无法显示“子菜单1”
使用对象,将变量作为参数传递
Main.java
static GUIConsole gui = new GUIConsole(new Main());
String subInput = null;
public void menuSwitch(String input) {
switch (input) {
case "1":
System.out.println("Case 1 Entered!");
gui.readConsole();
if (subInput.equalsIgnoreCase("test1")){
System.out.println("sub menu1");
}
break;
case "2":
System.out.println("Case 2 Entered!");
gui.readConsole();
if (subInput.equalsIgnoreCase("test2")){
System.out.println("sub menu2");
}
break;
case "3":
System.out.println("Case 2 Entered!");
gui.readConsole();
if (subInput.equalsIgnoreCase("test3")){
System.out.println("sub menu3");
}
break;
GUI.java
public class GUIConsole {
static Scanner in = new Scanner(System.in);
public static String input= null;
Main main;
public GUIConsole(Main main) {
this.main= main;
mainMenu();
readConsole();
}
public static void mainMenu() {
System.out.println("press 1 for case 1");
System.out.println("press 2 for case 2");
System.out.println("press 3 for case 3");
}
public void readConsole () {
System.out.println("entered readconsole");
input = in.nextLine();
readConsoleSort(input);
}
public void readConsoleSort(String input) {
if (input.length()==1) {
main.menuSwitch(input);
}else {
main.subInput = input;
}
}
expected : be able to go into submenus