JAVA获取免费天气

JAVA调用天气代码示例

前沿:最近在开发任务中需要获取每日的实时天气和天气预报,要求还是免费的。在网络上搜索了一下免费的API并有了以下思路

免费API网址:https://dev.qweather.com/docs/api/grid-weather/grid-weather-now/

  1. 调用格林天气API域名和正常的不同
  2. 需要注册账号需要得到key
  3. 上限:2000条/每日

image-20240327094031600

配置文件

weather:
  # 缓存时间【分钟】
  ttl: 30
  # 地区
  area: 北京
  # 格点天气配置
  gedian-url: https://devapi.qweather.com
  gedian-location: xxxx # 地区编码号
  gedian-key: xxxxxx

配置实体类

@ConfigurationProperties(
    prefix = "weather",
    ignoreUnknownFields = true // 排除未匹配字段
)
@Component
@Data
@ToString
public class WeatherProperties {
    private Integer ttl;
    private String area;
    private String gedianUrl;
    private String gedianLocation;
    private String gedianKey;
}

API结果返回类

没有提供所有的返回类

@Data
public class NowData {
    /**
     * 数据观测时间
     */
    @JsonProperty("obsTime")
    private String obsTime;

    /**
     * 温度,默认单位:摄氏度
     */
    @JsonProperty("temp")
    private String temp;

    /**
     * 天气状况的图标代码
     */
    @JsonProperty("icon")
    private String icon;

    /**
     * 天气状况的文字描述
     */
    @JsonProperty("text")
    private String text;

    /**
     * 风向360角度
     */
    @JsonProperty("wind360")
    private String wind360;

    /**
     * 风向
     */
    @JsonProperty("windDir")
    private String windDir;

    /**
     * 风力等级
     */
    @JsonProperty("windScale")
    private String windScale;

    /**
     * 风速,公里/小时
     */
    @JsonProperty("windSpeed")
    private String windSpeed;

    /**
     * 相对湿度,百分比数值
     */
    @JsonProperty("humidity")
    private String humidity;

    /**
     * 当前小时累计降水量
     */
    @JsonProperty("precip")
    private String precip;

    /**
     * 大气压强
     */
    @JsonProperty("pressure")
    private String pressure;

    /**
     * 云量,百分比数值。可能为空
     */
    @JsonProperty("cloud")
    private String cloud;

    /**
     * 露点温度。可能为空
     */
    @JsonProperty("dew")
    private String dew;

    /**
     * 最高气温
     */
    private String maxTemp;

    /**
     * 最低气温
     */
    private String minTemp;

    /**
     * 地区
     */
    private String area;

}

远程调用接口

需要在启动类上加@EnableFeignClients

@FeignClient(name = "weather", url = "${weather.gedian-url}")
public interface WeatherFeign {


    /**
     * 格点获取实时天气
     *
     * @param location 地区编码
     * @param key      请求key
     * @return 实时天气
     */
    @GetMapping("/v7/weather/now")
    WeatherNow getWeatherNow(@RequestParam("location") String location, @RequestParam("key") String key);

    /**
     * 格点获取3日天气预报
     *
     * @param location 地区编码
     * @param key      请求key
     * @return 3日天气预报
     */
    @GetMapping("/v7/weather/3d")
    WeatherForecast getWeatherDay3(@RequestParam("location") String location, @RequestParam("key") String key);


    /**
     * 格点获取当天生活指数
     *
     * @param location 地区编码
     * @param key      用户认证key
     * @param type     生活指数的类型ID
     * @return json
     */
    @GetMapping("/v7/indices/1d")
    Map<String, Object> getDailyLivingIndex(@RequestParam("location") String location, @RequestParam("key") String key, @RequestParam("type") String type);
}

控制层处理

其中的Redis工具类

@RestController
@RequestMapping("/weather")
public class WeatherController {

    @Resource
    private WeatherFeign weather;
    @Resource
    private WeatherProperties weatherProperties;


    /**
     * 获取实时天气
     */
    @SaIgnore
    @GetMapping("/getInfo")
    public WeatherNow getInfo() {
        String key = CacheConstants.WEATHER + "getInfo";
        // 获取缓存对象
        WeatherNow weatherCache = RedisUtils.getCacheObject(key);
        if (Objects.isNull(weatherCache)) {
            // 获取天气
            weatherCache = getWeather();
            // 缓存有效期30分钟
            RedisUtils.setCacheObject(key, weatherCache);
            RedisUtils.expire(key, Duration.ofMinutes(weatherProperties.getTtl()));
        }
        return weatherCache;
    }

    /**
     * 获取天气
     *
     * @return 天气返回类
     */
    private WeatherNow getWeather() {
        // 远程调用 -> 实时天气
        WeatherNow weatherNow = weather.getWeatherNow(weatherProperties.getGedianLocation(), weatherProperties.getGedianKey());

        if (!StringUtils.equals(weatherNow.getCode(), String.valueOf(HttpStatus.SUCCESS))) {
            // 返回空对象
            return null;
        } else {
            // 地区名称
            weatherNow.getNow().setArea(weatherProperties.getArea());
            // 远程调用 -> 天气预报
            getForecast(weatherNow.getNow());
            return weatherNow;
        }
    }

    /**
     * 获取天气预报
     * 最高温度,最低温度
     *
     * @param nowData 天气返回类
     */
    private void getForecast(NowData nowData) {
        WeatherForecast weatherDay3 = weather.getWeatherDay3(weatherProperties.getGedianLocation(), weatherProperties.getGedianKey());
        List<WeatherForecast.DailyWeatherData> dailyList = weatherDay3.getDaily();
        if (CollectionUtil.isNotEmpty(dailyList)) {
            // 获取当天日期
            LocalDate currentDate = LocalDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            String formattedDate = currentDate.format(formatter);

            // 3日天气预报 -筛选> 当天最高温度,最低温度
            Optional<WeatherForecast.DailyWeatherData> first = dailyList.stream()
                .filter(f -> StringUtils.equals(formattedDate, f.getFxDate())).findFirst();
            if (first.isPresent()) {
                nowData.setMaxTemp(first.get().getTempMax());
                nowData.setMinTemp(first.get().getTempMin());
            }
        }
    }

    /**
     * 格点获取当天生活指数
     *
     * @return map
     */
    @SaIgnore
    @GetMapping("/getDailyLivingIndex")
    public Map<String, Object> getDailyLivingIndex() {
        //缓存key
        String key = CacheConstants.WEATHER + "getDailyLivingIndex";

        //获取缓存
        Map<String, Object> resDailyLivingIndex = RedisUtils.getCacheObject(key);

        //查询缓存是否存在
        if (CollectionUtil.isEmpty(resDailyLivingIndex)) {
            resDailyLivingIndex = weather.getDailyLivingIndex(weatherProperties.getGedianLocation(), weatherProperties.getGedianKey(), "0");
            // 缓存有效期1天
            if (resDailyLivingIndex != null && !resDailyLivingIndex.isEmpty()) {
                RedisUtils.setCacheObject(key, resDailyLivingIndex, Duration.ofDays(1));
            }
        }

        return resDailyLivingIndex;
    }


    /**
     * 获取3日天气预报
     */
    @SaIgnore
    @GetMapping("/getWeatherDay3")
    public WeatherForecast getWeatherDay3() {
        String key = CacheConstants.WEATHER + "getWeatherDay3";
        // 获取缓存对象
        WeatherForecast weatherCache = RedisUtils.getCacheObject(key);
        if (Objects.isNull(weatherCache)) {
            // 获取天气
            weatherCache = weather.getWeatherDay3(weatherProperties.getGedianLocation(), weatherProperties.getGedianKey());
            // 缓存有效期30分钟
            RedisUtils.setCacheObject(key, weatherCache);
            RedisUtils.expire(key, Duration.ofDays(1));
        }
        return weatherCache;
    }
}

Redis工具类

@NoArgsConstructor(access = AccessLevel.PRIVATE)
@SuppressWarnings(value = {"unchecked", "rawtypes"})
public class RedisUtils {

    private static final RedissonClient CLIENT = SpringUtils.getBean(RedissonClient.class);

    /**
     * 限流
     *
     * @param key          限流key
     * @param rateType     限流类型
     * @param rate         速率
     * @param rateInterval 速率间隔
     * @return -1 表示失败
     */
    public static long rateLimiter(String key, RateType rateType, int rate, int rateInterval) {
        RRateLimiter rateLimiter = CLIENT.getRateLimiter(key);
        rateLimiter.trySetRate(rateType, rate, rateInterval, RateIntervalUnit.SECONDS);
        if (rateLimiter.tryAcquire()) {
            return rateLimiter.availablePermits();
        } else {
            return -1L;
        }
    }

    /**
     * 获取客户端实例
     */
    public static RedissonClient getClient() {
        return CLIENT;
    }

    /**
     * 发布通道消息
     *
     * @param channelKey 通道key
     * @param msg        发送数据
     * @param consumer   自定义处理
     */
    public static <T> void publish(String channelKey, T msg, Consumer<T> consumer) {
        RTopic topic = CLIENT.getTopic(channelKey);
        topic.publish(msg);
        consumer.accept(msg);
    }

    public static <T> void publish(String channelKey, T msg) {
        RTopic topic = CLIENT.getTopic(channelKey);
        topic.publish(msg);
    }

    /**
     * 订阅通道接收消息
     *
     * @param channelKey 通道key
     * @param clazz      消息类型
     * @param consumer   自定义处理
     */
    public static <T> void subscribe(String channelKey, Class<T> clazz, Consumer<T> consumer) {
        RTopic topic = CLIENT.getTopic(channelKey);
        topic.addListener(clazz, (channel, msg) -> consumer.accept(msg));
    }

    /**
     * 缓存基本的对象,Integer、String、实体类等
     *
     * @param key   缓存的键值
     * @param value 缓存的值
     */
    public static <T> void setCacheObject(final String key, final T value) {
        setCacheObject(key, value, false);
    }

    /**
     * 缓存基本的对象,保留当前对象 TTL 有效期
     *
     * @param key       缓存的键值
     * @param value     缓存的值
     * @param isSaveTtl 是否保留TTL有效期(例如: set之前ttl剩余90 set之后还是为90)
     * @since Redis 6.X 以上使用 setAndKeepTTL 兼容 5.X 方案
     */
    public static <T> void setCacheObject(final String key, final T value, final boolean isSaveTtl) {
        RBucket<T> bucket = CLIENT.getBucket(key);
        if (isSaveTtl) {
            try {
                bucket.setAndKeepTTL(value);
            } catch (Exception e) {
                long timeToLive = bucket.remainTimeToLive();
                setCacheObject(key, value, Duration.ofMillis(timeToLive));
            }
        } else {
            bucket.set(value);
        }
    }

    /**
     * 缓存基本的对象,Integer、String、实体类等
     *
     * @param key      缓存的键值
     * @param value    缓存的值
     * @param duration 时间
     */
    public static <T> void setCacheObject(final String key, final T value, final Duration duration) {
        RBatch batch = CLIENT.createBatch();
        RBucketAsync<T> bucket = batch.getBucket(key);
        bucket.setAsync(value);
        bucket.expireAsync(duration);
        batch.execute();
    }

    /**
     * 如果不存在则设置 并返回 true 如果存在则返回 false
     *
     * @param key   缓存的键值
     * @param value 缓存的值
     * @return set成功或失败
     */
    public static <T> boolean setObjectIfAbsent(final String key, final T value, final Duration duration) {
        RBucket<T> bucket = CLIENT.getBucket(key);
        return bucket.setIfAbsent(value, duration);
    }

    /**
     * 注册对象监听器
     * <p>
     * key 监听器需开启 `notify-keyspace-events` 等 redis 相关配置
     *
     * @param key      缓存的键值
     * @param listener 监听器配置
     */
    public static <T> void addObjectListener(final String key, final ObjectListener listener) {
        RBucket<T> result = CLIENT.getBucket(key);
        result.addListener(listener);
    }

    /**
     * 设置有效时间
     *
     * @param key     Redis键
     * @param timeout 超时时间
     * @return true=设置成功;false=设置失败
     */
    public static boolean expire(final String key, final long timeout) {
        return expire(key, Duration.ofSeconds(timeout));
    }

    /**
     * 设置有效时间
     *
     * @param key      Redis键
     * @param duration 超时时间
     * @return true=设置成功;false=设置失败
     */
    public static boolean expire(final String key, final Duration duration) {
        RBucket rBucket = CLIENT.getBucket(key);
        return rBucket.expire(duration);
    }

    /**
     * 获得缓存的基本对象。
     *
     * @param key 缓存键值
     * @return 缓存键值对应的数据
     */
    public static <T> T getCacheObject(final String key) {
        RBucket<T> rBucket = CLIENT.getBucket(key);
        return rBucket.get();
    }

    /**
     * 获得key剩余存活时间
     *
     * @param key 缓存键值
     * @return 剩余存活时间
     */
    public static <T> long getTimeToLive(final String key) {
        RBucket<T> rBucket = CLIENT.getBucket(key);
        return rBucket.remainTimeToLive();
    }

    /**
     * 删除单个对象
     *
     * @param key 缓存的键值
     */
    public static boolean deleteObject(final String key) {
        return CLIENT.getBucket(key).delete();
    }

    /**
     * 删除集合对象
     *
     * @param collection 多个对象
     */
    public static void deleteObject(final Collection collection) {
        RBatch batch = CLIENT.createBatch();
        collection.forEach(t -> {
            batch.getBucket(t.toString()).deleteAsync();
        });
        batch.execute();
    }

    /**
     * 检查缓存对象是否存在
     *
     * @param key 缓存的键值
     */
    public static boolean isExistsObject(final String key) {
        return CLIENT.getBucket(key).isExists();
    }

    /**
     * 缓存List数据
     *
     * @param key      缓存的键值
     * @param dataList 待缓存的List数据
     * @return 缓存的对象
     */
    public static <T> boolean setCacheList(final String key, final List<T> dataList) {
        RList<T> rList = CLIENT.getList(key);
        return rList.addAll(dataList);
    }

    /**
     * 注册List监听器
     * <p>
     * key 监听器需开启 `notify-keyspace-events` 等 redis 相关配置
     *
     * @param key      缓存的键值
     * @param listener 监听器配置
     */
    public static <T> void addListListener(final String key, final ObjectListener listener) {
        RList<T> rList = CLIENT.getList(key);
        rList.addListener(listener);
    }

    /**
     * 获得缓存的list对象
     *
     * @param key 缓存的键值
     * @return 缓存键值对应的数据
     */
    public static <T> List<T> getCacheList(final String key) {
        RList<T> rList = CLIENT.getList(key);
        return rList.readAll();
    }

    /**
     * 缓存Set
     *
     * @param key     缓存键值
     * @param dataSet 缓存的数据
     * @return 缓存数据的对象
     */
    public static <T> boolean setCacheSet(final String key, final Set<T> dataSet) {
        RSet<T> rSet = CLIENT.getSet(key);
        return rSet.addAll(dataSet);
    }

    /**
     * 注册Set监听器
     * <p>
     * key 监听器需开启 `notify-keyspace-events` 等 redis 相关配置
     *
     * @param key      缓存的键值
     * @param listener 监听器配置
     */
    public static <T> void addSetListener(final String key, final ObjectListener listener) {
        RSet<T> rSet = CLIENT.getSet(key);
        rSet.addListener(listener);
    }

    /**
     * 获得缓存的set
     *
     * @param key 缓存的key
     * @return set对象
     */
    public static <T> Set<T> getCacheSet(final String key) {
        RSet<T> rSet = CLIENT.getSet(key);
        return rSet.readAll();
    }

    /**
     * 缓存Map
     *
     * @param key     缓存的键值
     * @param dataMap 缓存的数据
     */
    public static <T> void setCacheMap(final String key, final Map<String, T> dataMap) {
        if (dataMap != null) {
            RMap<String, T> rMap = CLIENT.getMap(key);
            rMap.putAll(dataMap);
        }
    }

    /**
     * 注册Map监听器
     * <p>
     * key 监听器需开启 `notify-keyspace-events` 等 redis 相关配置
     *
     * @param key      缓存的键值
     * @param listener 监听器配置
     */
    public static <T> void addMapListener(final String key, final ObjectListener listener) {
        RMap<String, T> rMap = CLIENT.getMap(key);
        rMap.addListener(listener);
    }

    /**
     * 获得缓存的Map
     *
     * @param key 缓存的键值
     * @return map对象
     */
    public static <T> Map<String, T> getCacheMap(final String key) {
        RMap<String, T> rMap = CLIENT.getMap(key);
        return rMap.getAll(rMap.keySet());
    }

    /**
     * 获得缓存Map的key列表
     *
     * @param key 缓存的键值
     * @return key列表
     */
    public static <T> Set<String> getCacheMapKeySet(final String key) {
        RMap<String, T> rMap = CLIENT.getMap(key);
        return rMap.keySet();
    }

    /**
     * 往Hash中存入数据
     *
     * @param key   Redis键
     * @param hKey  Hash键
     * @param value 值
     */
    public static <T> void setCacheMapValue(final String key, final String hKey, final T value) {
        RMap<String, T> rMap = CLIENT.getMap(key);
        rMap.put(hKey, value);
    }

    /**
     * 获取Hash中的数据
     *
     * @param key  Redis键
     * @param hKey Hash键
     * @return Hash中的对象
     */
    public static <T> T getCacheMapValue(final String key, final String hKey) {
        RMap<String, T> rMap = CLIENT.getMap(key);
        return rMap.get(hKey);
    }

    /**
     * 删除Hash中的数据
     *
     * @param key  Redis键
     * @param hKey Hash键
     * @return Hash中的对象
     */
    public static <T> T delCacheMapValue(final String key, final String hKey) {
        RMap<String, T> rMap = CLIENT.getMap(key);
        return rMap.remove(hKey);
    }

    /**
     * 删除Hash中的数据
     *
     * @param key   Redis键
     * @param hKeys Hash键
     */
    public static <T> void delMultiCacheMapValue(final String key, final Set<String> hKeys) {
        RBatch batch = CLIENT.createBatch();
        RMapAsync<String, T> rMap = batch.getMap(key);
        for (String hKey : hKeys) {
            rMap.removeAsync(hKey);
        }
        batch.execute();
    }

    /**
     * 获取多个Hash中的数据
     *
     * @param key   Redis键
     * @param hKeys Hash键集合
     * @return Hash对象集合
     */
    public static <K, V> Map<K, V> getMultiCacheMapValue(final String key, final Set<K> hKeys) {
        RMap<K, V> rMap = CLIENT.getMap(key);
        return rMap.getAll(hKeys);
    }

    /**
     * 设置原子值
     *
     * @param key   Redis键
     * @param value 值
     */
    public static void setAtomicValue(String key, long value) {
        RAtomicLong atomic = CLIENT.getAtomicLong(key);
        atomic.set(value);
    }

    /**
     * 获取原子值
     *
     * @param key Redis键
     * @return 当前值
     */
    public static long getAtomicValue(String key) {
        RAtomicLong atomic = CLIENT.getAtomicLong(key);
        return atomic.get();
    }

    /**
     * 递增原子值
     *
     * @param key Redis键
     * @return 当前值
     */
    public static long incrAtomicValue(String key) {
        RAtomicLong atomic = CLIENT.getAtomicLong(key);
        return atomic.incrementAndGet();
    }

    /**
     * 递减原子值
     *
     * @param key Redis键
     * @return 当前值
     */
    public static long decrAtomicValue(String key) {
        RAtomicLong atomic = CLIENT.getAtomicLong(key);
        return atomic.decrementAndGet();
    }

    /**
     * 获得缓存的基本对象列表
     *
     * @param pattern 字符串前缀
     * @return 对象列表
     */
    public static Collection<String> keys(final String pattern) {
        Stream<String> stream = CLIENT.getKeys().getKeysStreamByPattern(pattern);
        return stream.collect(Collectors.toList());
    }

    /**
     * 删除缓存的基本对象列表
     *
     * @param pattern 字符串前缀
     */
    public static void deleteKeys(final String pattern) {
        CLIENT.getKeys().deleteByPattern(pattern);
    }

    /**
     * 检查redis中是否存在key
     *
     * @param key 键
     */
    public static Boolean hasKey(String key) {
        RKeys rKeys = CLIENT.getKeys();
        return rKeys.countExists(key) > 0;
    }
}

maven

<!--feign客户端依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>3.1.3</version> <!-- 或者根据相应版本选择合适的OpenFeign版本 -->
        </dependency>
().getKeysStreamByPattern(pattern);
        return stream.collect(Collectors.toList());
    }

    /**
     * 删除缓存的基本对象列表
     *
     * @param pattern 字符串前缀
     */
    public static void deleteKeys(final String pattern) {
        CLIENT.getKeys().deleteByPattern(pattern);
    }

    /**
     * 检查redis中是否存在key
     *
     * @param key 键
     */
    public static Boolean hasKey(String key) {
        RKeys rKeys = CLIENT.getKeys();
        return rKeys.countExists(key) > 0;
    }
}

maven

<!--feign客户端依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>3.1.3</version> <!-- 或者根据相应版本选择合适的OpenFeign版本 -->
        </dependency>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/492100.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

工程企业的未来选择:Java版工程项目管理系统平台与数字化管理的融合

在现代化的工程项目管理中&#xff0c;一套功能全面、操作便捷的系统至关重要。本文将介绍一个基于Spring Cloud和Spring Boot技术的Java版工程项目管理系统&#xff0c;结合Vue和ElementUI实现前后端分离。该系统涵盖了项目管理、合同管理、预警管理、竣工管理、质量管理等多个…

基于Colab训练的yolov4-tiny自定义数据集(可用于OpenCV For Unity)

参考资料文档和视频。 1.打开文档,点击【文件】【在云端硬盘中保存一份副本】,即将文档复制到自己云端硬盘。 2.打开该文件,按文中提示进行。 【代码执行程序】【更改运行时类型】修改运行时为GPU(免费的GPU不好用,收费的好用,某宝上几十元就可用一个月) 步骤1) !git…

大数据面试题 —— Kafka

目录 消息队列 / Kafka 的好处消息队列的两种模式什么是 KafkaKafka 优缺点你在哪些场景下会选择 Kafka讲下 Kafka 的整体结构Kafka 工作原理 / 流程Kafka为什么那么快/高效读写的原因 / 实现高吞吐的原理生产者如何提高吞吐量&#xff08;调优&#xff09;kafka 消息数据积压&…

python爬虫-----输入输出与流程控制语句(第四天)

&#x1f388;&#x1f388;作者主页&#xff1a; 喔的嘛呀&#x1f388;&#x1f388; &#x1f388;&#x1f388;所属专栏&#xff1a;python爬虫学习&#x1f388;&#x1f388; ✨✨谢谢大家捧场&#xff0c;祝屏幕前的小伙伴们每天都有好运相伴左右&#xff0c;一定要天天…

力扣:字母迷宫,python

这里写自定义目录标题 问题描述题解踩坑记录global和nonlocal关键字的区别&#xff1a;类中可以用实例变量替换全局变量 问题描述 字母迷宫游戏初始界面记作 m x n 二维字符串数组 grid&#xff0c;请判断玩家是否能在 grid 中找到目标单词 target。 注意&#xff1a;寻找单词…

【C++ leetcode】双指针(专题完结)

15. 三数之和 题目 给你一个整数数组 nums &#xff0c;判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i ! j、i ! k 且 j ! k &#xff0c;同时还满足 nums[i] nums[j] nums[k] 0 。请 你返回所有和为 0 且不重复的三元组。 注意&#xff1a;答案中不可以包含重复的…

P1443马的遍历 典bfs

题目&#xff1a; 代码&#xff1a; #include<algorithm> #include<iostream> #include<cstring> #include<queue>using namespace std;int n,m;int dx[] {-1,-1,-2,-2,1,1,2,2}; int dy[] {-2,2,-1,1,-2,2,-1,1}; bool vis[450][450];struct node{…

秋招打卡算法题第一天

一年多没有刷过算法题了&#xff0c;已经不打算找计算机类工作了&#xff0c;但是思来想去&#xff0c;还是继续找吧。 1. 字符串最后一个单词的长度 public static void main(String[] args) {Scanner in new Scanner(System.in);while(in.hasNextInt()){String itemin.nextL…

Ubuntu 下统计文件数量的命令

参考:https://blog.csdn.net/kxh123456/article/details/123811580 查看当前目录下的文件数量&#xff08;不包含子目录中的文件&#xff09; ls -l|grep "^-"| wc -l实例展示&#xff1a;如下图所示&#xff0c;当前路径下&#xff0c;有2个json文件和2个文件夹&a…

面向对象【Annotation注解】

文章目录 注解概述注解与注释常见的 Annotation最基本的注解使用@Override@Override@SuppressWarnings元注解@Retention@Target@Documented@Inherited自定义注解格式定义使用注解概述 注解(Annotation)是从 JDK5.0 开始引入,以“@注解名”在代码中存在。例如: @Override @D…

EtherCAT转RS232网关在风电领域的应用

开疆智能EtherCAT转RS232网关在风电领域的应用主要体现在以下几个方面&#xff1a; 1.数据采集与传输&#xff1a;在风力发电设备中&#xff0c;传感器和执行器的数据采集和传输至关重要。EtherCAT转RS232网关可以将风力发电设备中的RS232通信协议转换为EtherCAT协议&#xff0…

Unity 布局元素Layout Element

Layout Element是一种用于控制UI元素在布局组件&#xff08;如Horizontal Layout Group、Vertical Layout Group、Grid Layout Group、Content Size Fitter和Aspect Ratio Fitter&#xff09;中的大小和位置的组件。Layout Element组件可以附加到UI元素上&#xff0c;以便在布局…

操作系统—信号量和条件变量实践

文章目录 信号量和条件变量实践1.实验基本环境(1).基本系统环境 2.信号量(1).如何使用信号量?(2).课上的例子(3).打印合法括号序列(4).打印很多条鱼 3.条件变量(1).为什么选择条件变量?(2).还是课上的例子(3).还是合法括号序列 (4).还是打印很多鱼总结 参考资料 信号量和条件…

设计模式之装饰模式解析

装饰模式 1&#xff09;概述 1.定义 动态地给一个对象增加一些额外的职责&#xff0c;在增加对象功能时&#xff0c;装饰模式比生成子类实现更为灵活。 2.作用 装饰模式可以在不改变一个对象本身功能的基础上给对象增加额外的新行为。 3.结构图 4.角色 Component&#xf…

编程语言|C语言——C语言标识符的命名规则

1.标识符简介 在计算机高级语言中&#xff0c;用来对变量、符号常量名、函数、数组、类型等命名的有效字符序列统称为标识符。标识符可以简单认为是一个名字&#xff0c;用来标识变量名、常量名、函数名及数组等。变量名a、b、c,符号常量名PI、Pai,函数名printf、scanf等都是标…

题目:忐忑楼梯Ⅱ

问题描述&#xff1a; 解题思路&#xff1a; 利用差分&#xff0c;当第一个以后的差分元素都为零时就代表楼梯高度等于第一个楼梯的高度。为什么是第一个呢&#xff0c;因为以第一个为标准的区间操作数最少。 注意点&#xff1a;每次都只能加一或减一&#xff0c;ans开ll 题解&…

港澳青年看祖国—千名青年创业家内地暨江港青年交流活动在江举行

为聚焦“一点两地”全新定位&#xff0c;助力纵深推进新阶段粤港澳大湾区建设&#xff0c;3月22日&#xff0c;江门市委统战部、团市委、市青联联合香港深水埗区青年发展及公民教育委员会、愿景基金会、香港青年创业家总商会举办千名青年创业家内地行暨江港青年交流活动&#x…

2024/3/27打卡接龙数列——动态规划(线性DP/最长上升子序列)

目录 题目 思路 代码 题目 思路 这个题求最少删除多个个数&#xff0c;其实是求最长的接龙数列&#xff0c;用总个数-接龙数列的个数就是最少删除的个数。 那么如何求解最长的接龙数列的问题。 思考&#xff0c;每个数都有选或不选的两种选项&#xff08;选&#xff1a;可以…

【Java程序设计】【C00384】基于(JavaWeb)Springboot的民航网上订票系统(有论文)

【C00384】基于&#xff08;JavaWeb&#xff09;Springboot的民航网上订票系统&#xff08;有论文&#xff09; 项目简介项目获取开发环境项目技术运行截图 博主介绍&#xff1a;java高级开发&#xff0c;从事互联网行业六年&#xff0c;已经做了六年的毕业设计程序开发&#x…

Pillow教程04:学习ImageDraw+Font字体+alpha composite方法,给图片添加文字水印

---------------Pillow教程集合--------------- Python项目18&#xff1a;使用Pillow模块&#xff0c;随机生成4位数的图片验证码 Python教程93&#xff1a;初识Pillow模块&#xff08;创建Image对象查看属性图片的保存与缩放&#xff09; Pillow教程02&#xff1a;图片的裁…
最新文章