0
点赞
收藏
分享

微信扫一扫

Java、对三个城市排序

雨鸣静声 2022-01-20 阅读 86

编写一个程序,提示用户输入三个城市名称,然后以升序进行显示。


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("[\\[\\],]", " "));  
                     //正则表达式替换'['、']'、','为空格
		}
	}

}

 

举报

相关推荐

0 条评论