Continuous input commands

為{幸葍}努か 提交于 2019-12-06 16:09:22

This is a simple way to implement an input loop:

Scanner sc = new Scanner(System.in);

for (prompt(); sc.hasNextLine(); prompt()) {

    String line = sc.nextLine().replaceAll("\n", "");

    // return pressed
    if (line.length == 0) 
        continue;

    // split line into arguments
    String[] args = line.split(" ");    

    // process arguments
    if (args.length == 1) {
        if (args[0].equalsIgnoreCase("exit"))
            System.exit(0);
        if (args[0].equalsIgnoreCase("dosomething"))
            // do something
    } else if (args.length == 2) {
        // do stuff with parameters
    } 
}

Assuming prompt() prints out the prompt here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!