🔍
📢

Java控制台Scanner类获取用户输入及优雅退出

import java.util.Scanner;
public class MainClass {

    public static void main(String[] args) {
        System.out.println("Input q to exit.");
        try (Scanner input = new Scanner(System.in)) {
            while (true) {
                String line = input.nextLine();
                if (line.equalsIgnoreCase("q")) {
                    break;
                }
                System.out.println(">>>" + line);
            }
        }
        System.out.println("exited.");
    }

}