//3、编写程序,循环接收用户从键盘输入多个字符串,直到输入“end”时循环结束,并将所有已输入的字符串按字典顺序倒序打印。
public class ExamTest3 {
public static void main(String[] args) {
TreeSet<String> ts = new TreeSet<>(new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return -(s1.compareTo(s2));
}
});
while (true) {
Scanner sc = new Scanner(System.in);
String string = sc.nextLine();
if (string.equals("end")) {
Iterator<String> it = ts.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
break;
}
ts.add(string);
}
}
} |
|