Java命令提示循环

时间:2015-04-27 13:08:58

标签: java while-loop

我有这个简单的代码,就像一个小命令提示符。要求用户输入命令,并根据他输入的内容显示消息。我只想让它循环,以便要求用户一次又一次地输入命令,直到他键入'exit'。我怎么做? 这是代码:

public static void main(String[] args) {

    Scanner command = new Scanner(System.in);

    System.out.println("Enter command: ");

    String text = command.nextLine();

        switch(text){

        case "start":
            System.out.println("Machine started!");
            break;

        case "stop":
            System.out.println("Machine stopped.");
            break;

        case "exit":
            System.out.println("Application Closed");
            break;

        default:
            System.out.println("Command not recognized!");
            break;
        }

    command.close();
}

谢谢

5 个答案:

答案 0 :(得分:3)

你可以把它放在像这样的while循环中:

String text = "";

while(!text.equalsIgnoreCase("exit"))
{
    System.out.println("Enter command: ");

    text = command.nextLine();

        switch(text){

        case "start":
            System.out.println("Machine started!");
            break;

        case "stop":
            System.out.println("Machine stopped.");
            break;

        case "exit":
            System.out.println("Application Closed");
            break;

        default:
            System.out.println("Command not recognized!");
            break;
        }
}

让while循环的条件为text<> “退出”。

答案 1 :(得分:2)

I prefer:

public static void main(String[] args) {

    Scanner command = new Scanner(System.in);

    System.out.println("Enter command: ");
    boolean running = true;

    while(running){

        switch(command.nextLine()){

        case "start":
            System.out.println("Machine started!");
            break;

        case "stop":
            System.out.println("Machine stopped.");
            break;

        case "exit":
            System.out.println("Application Closed");
            running = false;
            break;

        default:
            System.out.println("Command not recognized!");
            break;
        }
    }
    command.close();
}

答案 2 :(得分:1)

Something like this? I prefer do-while for this type of situation because you probably want to give at least one command.

public static void main(String[] args) {
    boolean running = true;

    Scanner command = new Scanner(System.in);
    do {
        System.out.println("Enter command: ");
        String text = command.nextLine();
        switch(text){
            case "start":
                System.out.println("Machine started!");
                break;
            case "stop":
                System.out.println("Machine stopped.");
                running = false;  //here?
                break;
            case "exit":
                System.out.println("Application Closed");
                running = false; //..or here?
                break;
            default:
                System.out.println("Command not recognized!");
                break;
        }
    } while(running);
    command.close();
}

答案 3 :(得分:0)

在初始示例中缺少循环:

    public static void main(String args[]) {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    while (true) {
        String command = br.readLine().toLowerCase();

        switch (command) {
            case "exit": {
                // exit here
            } break;

            default: {
                // unrecognised command
            }
        }
    }
}

答案 4 :(得分:0)

If you want to use a switch, easiest way would probably be a simple infinite while-loop with a label. When the input matches "exit", we just break the loop.

loop:
while (true) {
    String text = command.nextLine();
    switch (text) {
    // ...
    case "exit":
        break loop;
    }
}
相关问题