Java - user interface and termination
For a project I need two wirte a java program with a user interface. Not GUI. Only a user interface asking the user to enter a command. The user should also be able to terminate the program. Do someone maybe have a link to a website that might help - I'm not sure in which direction开发者_运维问答 to look...
It's not a tutorial but here's a basic example of what you describe to get you started. My app here takes two commands 'hello' which prints a greeting, and 'quit' which exits the program. Any other input will display 'unknown command'.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String command;
do {
command = reader.readLine();
if (command.equals("hello")) {
System.out.println("Hello there!");
} else if (command.equals("quit")) {
return; // exit program
} else {
System.out.println("Unknown command.");
}
} while (true);
}
}
Java IO Tutorial
Basic input and output is all you need.
Specifically, I/O from the Command Line
A console program? You can read user input using Console.readLine(). The Console class has a very detailed javadoc.
精彩评论