不断输入字符串,直到输入"exit"后退出,并按顺序出输入的所有打印字符串

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_40567229/article/details/85164773

class hello {
    public static void main(String[] args) throws ParseException {
        Scanner sc = new Scanner(System.in);
        TreeSet<String> ts = new TreeSet<>(new Comparator<String>() {

            @Override
            public int compare(String c1,String c2) {
                // TODO Auto-generated method stub
                int result = c1.compareTo(c2);
                return result == 0?1:result;
            }
        });
        
        while( true ) {
            String s = sc.nextLine();
            if(s.equals("exit")){
                break;
            }
            ts.add(s);
        }
        System.out.println(ts);

输出结果:

猜你喜欢

转载自blog.csdn.net/weixin_40567229/article/details/85164773