我希望它看起来好像用户输入正在由机器以类似循环的方式响应,回到原始问题并允许先前的答案被接受为可行的答案。
import java.util.Scanner;
public class Application {
public static void main(String[] args) {
Scanner input = new Scanner( System.in);
System.out.println("Please enter a command: ");
String text = input.nextLine();
switch (text) {
case "start":
System.out.println("Machine Started!");
break;
case "stop":
System.out.println("Machine Stopped.");
break;
case "sleep":
System.out.println("In progress: sleeping...");
break;
case "transform":
System.out.println("In progress: transforming...");
break;
default:
System.out.println("Command not recognized");
}
}
}
答案 0 :(得分:0)
import java.util.Scanner;
public class Application {
public static void main(String[] args) {
Scanner input = new Scanner( System.in);
String text;
do {
System.out.println("Please enter a command: ");
text = input.nextLine();
switch (text) {
case "start":
System.out.println("Machine Started!");
break;
case "stop":
System.out.println("Machine Stopped.");
break;
case "sleep":
System.out.println("In progress: sleeping...");
break;
case "transform":
System.out.println("In progress: transforming...");
break;
default:
System.out.println("Command not recognized");
}
} while (!text.equalsIgnoreCase("quit"));
}
}
这应该是你要找的。当用户输入退出时,程序将中止