Java 编写 省市县并提取他的首字母大写 并展示他下面的所有信息
1条回答 默认 最新
恐怖如斯恐怖如斯啊 2024-12-01 09:23关注以下回复参考:皆我百晓生、券券喵儿等免费微信小程序作答:
这是一个基本的Java程序,用于创建一个省市县结构并提取它们的首字母大写。然后展示它们下面的所有信息。我们可以创建一个类来表示地理位置(包括省、市和县),并使用集合来存储和管理这些对象。以下是一个简单的实现:
首先,创建一个表示地理位置的类:
public class Location { private String province; private String city; private String county; public Location(String province, String city, String county) { this.province = province; this.city = city; this.county = county; } public String getProvince() { return province; } public String getCity() { return city; } public String getCounty() { return county; } // 将省份、城市和县的名称首字母大写的方法 public void capitalize() { this.province = province.substring(0, 1).toUpperCase() + province.substring(1); this.city = city.substring(0, 1).toUpperCase() + city.substring(1); this.county = county.substring(0, 1).toUpperCase() + county.substring(1); } }然后,我们可以创建一个主类来创建地理位置对象并展示它们的信息:
import java.util.*; public class Main { public static void main(String[] args) { // 创建省市县对象并添加到列表中 List<Location> locations = new ArrayList<>(); locations.add(new Location("广东", "广州", "越秀")); locations.add(new Location("浙江", "杭州", "西湖")); locations.add(new Location("江苏", "南京", "玄武")); // 更多省市县... // 遍历列表并展示信息,同时提取首字母大写 for (Location location : locations) { location.capitalize(); // 将该位置的名称首字母大写 System.out.println("Province: " + location.getProvince()); System.out.println("City: " + location.getCity()); System.out.println("County: " + location.getCounty()); System.out.println("All information: " + location.getProvince() + "-" + location.getCity() + "-" + location.getCounty()); System.out.println(); // 换行以区分不同的地理位置信息 } } }在这个例子中,我们创建了一个简单的
Location类来表示地理位置,并在主类中创建了一些省市县对象并存储在一个列表中。然后,我们遍历这个列表,打印出每个地理位置的信息,并使用capitalize()方法将每个名称的首字母大写。本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报