编写一个程序,提示用户输入三个城市名称,然后以升序进行显示。
package pack2;
import java.util.Scanner;
import java.util.TreeSet;
public class ThreeCity {
public static void main(String[] args) {
try(Scanner input = new Scanner(System.in);) {
TreeSet<String> cities = new TreeSet<>(); //创建树集对象(自动升序排序)
System.out.print("Enter the first city: ");
cities.add(input.nextLine());
System.out.print("Enter the second city: ");
cities.add(input.nextLine());
System.out.print("Enter the third city: ");
cities.add(input.nextLine());
System.out.println("The three cities in alphabetical order"+
"are"+cities.toString().replaceAll("[\\[\\],]", " "));
//正则表达式替换'['、']'、','为空格
}
}
}