[英]Java command prompt loop
I have this simple code that works like a small command prompt. 我有这个简单的代码,就像一个小的命令提示符。 User is asked to enter a command and is displayed a message according to what he entered.
要求用户输入命令,并根据输入内容显示一条消息。 I just want to make it loop so that the user is asked to enter a command again and again and again until he types 'exit'.
我只想使其循环,以便要求用户一次又一次地输入命令,直到他键入“退出”为止。 How do I do that?
我怎么做? Here is the code:
这是代码:
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();
}
Thank you 谢谢
You can put it in a while loop like this: 您可以将其放入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;
}
}
Just let the condition for your while loop be while text <> "exit". 只要让您的while循环条件为while文本<>“ exit”。
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();
}
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();
}
while loop is missing in the initial example: 最初的示例中缺少while循环:
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
}
}
}
}
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. 当输入匹配
"exit"
,我们就中断了循环。
loop:
while (true) {
String text = command.nextLine();
switch (text) {
// ...
case "exit":
break loop;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.