遍历处理程序类对象以读取控制台

时间:2019-05-30 09:24:41

标签: java

您可能已经看过我先前的问题,它是基于处理程序的实现而构建的。但是我发现很难使其正常工作。

我有三个班级:

main.java -一种简单的方法,它要求读取控制台,然后输出用户输入。

handler.java -从gui.java请求“ readconsole”

gui.java -显示gui -读取控制台

感觉就像我错过了一些简单的东西!

main.java

public class main {

    static handler handler = new handler();

    public static void main(String[] args) {

    }

    public static void menuSwitch(String input)  {

        System.out.println("entered menu switch with input " +input);
        String s = handler.requestReadConsole();
        System.out.println(s);

}
}

handler.java

public class handler {

    static gui gui = new gui();

    static String inputvariable= null;

    public handler() {
        requestAndSortReadConsole();
    }


    public String requestReadConsole() {
        System.out.println("entered read console");
        String s = gui.readConsole();
        return s;
    }


    public String requestAndSortReadConsole() {
        System.out.println("entered requestAndSortReadConsole");
        sortInput(requestReadConsole());

        System.out.println("sorted value = " + inputvariable);
        return inputvariable;
    }



    public void sortInput(String input) {
        System.out.println("entered sort input with input = " + input);

        if (input.length() == 1) {  
            System.out.println("input length EQUALS 1");
            main.menuSwitch(input);         
        }else {

            inputvariable = input;
            System.out.println(inputvariable);
        }
        System.out.println("return input");
    }
    }

gui.java

public class gui {

    static Scanner in = new Scanner(System.in);

    public gui() {
        System.out.println("main menu called");
        mainMenu();

    }

    public 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 String readConsole () {
        String input = null;
        System.out.println("entered readconsole gui");
        input = in.nextLine();
        System.out.println("input  = " + input);
        in.close();
        return input;
    }






}

方法menuSwitch应该输出变量s,但是它不输出任何内容,并继续允许用户输入到控制台!

0 个答案:

没有答案