Copy the code code as follows:
public static void main(String[] args) {
System.out.println("Please enter a value and end with Enter:");
Scanner sc = new Scanner(System.in);
sc.useDelimiter("/n");
while (sc.hasNext()) {
String s = sc.next();
System.out.println("Get value: "+s);
}
}
1. Use the Scanner class to encapsulate the system.in input stream
2.sc.userDelimiter("/n"); Set the end of input flag, and end with a carriage return.
3. Use sc.hasNext() to determine whether there is content, and if there is, use sc.next() to take it out.