清理命令行提示符(Java)的打印

时间:2015-07-25 22:28:46

标签: java io coding-style

我试图用Java编写交互式提示。更具体地说,如下所示:

>>> load names;
>>> print names;

(即它在每一行打印>>>,然后用户输入命令。我编写了以下Java代码来完成此任务:

public static void main(String[] args) {
    try {
        BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
        String command;
        System.out.print(">>> ");
        while ((command = r.readLine()) != null) {
            processCommand(command);
            System.out.print(">>> ");
        }
    } catch (IOException e) {
        System.out.println("Something went wrong.");
    }
}

我的问题:有更清洁的方法吗?我不喜欢在多个地方打印提示符(>>>)的想法,我觉得应该有一个简单的方法来完成一次。

有关清理的建议吗?

2 个答案:

答案 0 :(得分:1)

这样做怎么样

<img src="http://img.freepik.com/free-photo/background-with-white-squares-free-vector_23
-2147501484.jpg?size=338c&ext=jpg" class="img-responsive" style="margin:0 auto;" >

答案 1 :(得分:0)

您可以添加到processComandSystem.out.println(">>>");

的底部
try {
    BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
    String command = " ";
    do{
        command = r.readLine();
        processCommand(command);
    }while (command != null) 
 } catch (IOException e) {
     System.out.println("Something went wrong.");
 }