Java 命令提示符循环
Java command prompt 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();
}
谢谢
你可以像这样把它放在一个 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 循环的条件为 while text <> "exit"。
初始示例中缺少 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
}
}
}
}
如果您想使用开关,最简单的方法可能是使用带有标签的简单无限 while 循环。当输入匹配 "exit"
时,我们就打破循环。
loop:
while (true) {
String text = command.nextLine();
switch (text) {
// ...
case "exit":
break loop;
}
}
我更喜欢:
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();
}
是这样的吗?对于这种情况,我更喜欢 do-while,因为您可能想要至少给出一个命令。
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();
}
我有这个简单的代码,就像一个小的命令提示符。要求用户输入命令,并根据他输入的内容显示一条消息。我只是想让它循环,以便要求用户一次又一次地输入命令,直到他键入 '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();
}
谢谢
你可以像这样把它放在一个 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 循环的条件为 while text <> "exit"。
初始示例中缺少 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
}
}
}
}
如果您想使用开关,最简单的方法可能是使用带有标签的简单无限 while 循环。当输入匹配 "exit"
时,我们就打破循环。
loop:
while (true) {
String text = command.nextLine();
switch (text) {
// ...
case "exit":
break loop;
}
}
我更喜欢:
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();
}
是这样的吗?对于这种情况,我更喜欢 do-while,因为您可能想要至少给出一个命令。
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();
}