栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java第十六次作业:使用阿里云提供的API查询天气预报

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java第十六次作业:使用阿里云提供的API查询天气预报

废话不多说,上代码!

package com.imooc.weather;

import com.imooc.weather.impl.WeatherUtilsImpl;

import java.util.List;
import java.util.Scanner;

public class Application {
    public static void main(String[] args) {
        System.out.println("查询最近天气预报:");
        System.out.println("输入1:查询未来24小时天气预报:");
        System.out.println("输入2:查询未来3天天气预报");
        System.out.println("输入3:查询未来7天天气预报");
        System.out.print("请输入您的选择:");
        Scanner scanner = new Scanner(System.in);
        int i = scanner.nextInt();
        System.out.println("用户输入数字:" + i);
        if (i==1){
            System.out.print("请输入城市名称查询未来24小时天气预报:");
            String city = scanner.next();
            WeatherUtils weatherUtils = new WeatherUtilsImpl();
            List weatherList = weatherUtils.w24h("API",city);//API输入自己的api应用程序编程接口,这里就不提供了!
            System.out.println(weatherList);
            if (weatherList.size() == 0){
                System.out.println("抱歉,未收录您查询的城市天气数据。");
            }else{
                for (HourWeather hourWeather : weatherList){
                    String template = "%s月%s日%s时|%-3s|%-20s|%-8s|%-4s℃";
                    String row = String.format(template,new String[]{
                            hourWeather.getMonth(),
                            hourWeather.getDay(),
                            hourWeather.getHour(),
                            hourWeather.getWindDirection(),
                            hourWeather.getWindPower(),
                            hourWeather.getWeather(),
                            hourWeather.getTemperature()
                    });
                    System.out.println(row);
                }
            }
        } else if (i==2){
            System.out.print("请输入城市名称查询未来3天天气预报:");
            String city = scanner.next();
            WeatherUtils weatherUtils = new WeatherUtilsImpl();
            List weatherList = weatherUtils.w3d("api", city);//API输入自己的api应用程序编程接口
            System.out.println(weatherList.size());
            if (weatherList.size() == 0) {
                System.out.println("抱歉,未收录您查询的城市天气数据。");
            }else{
                for (DayWeather weather : weatherList){
                    String template = "%-2s月%-2s日 | 气温: %s℃(日) %s℃(夜) | 天气: %s(日) %s(夜) | 风力: %s(日) %s(夜)";
                    String row = String.format(template,new String[]{
                            weather.getMonth(),
                            weather.getDay(),
                            weather.getDayAirTemperature(),
                            weather.getNightAirTemperature(),
                            weather.getDayWeather(),
                            weather.getNightWeather(),
                            weather.getDayWindPower(),
                            weather.getNightWindPower()
                            });
                    System.out.println(row);
                }
            }
            }
        else if (i==3) {
            System.out.print("请输入城市名称查询未来7天天气预报:");
            String city = scanner.next();
            WeatherUtils weatherUtils = new WeatherUtilsImpl();
            List weatherList = weatherUtils.w7d("api", city);//API输入自己的api应用程序编程接口
            System.out.println(weatherList.size());
            if (weatherList.size() == 0) {
                System.out.println("抱歉,未收录您查询的城市天气数据。");
            } else {
                for (DayWeather weather : weatherList) {
                    String template = "%-2s月%-2s日 | 气温: %s℃(日) %s℃(夜) | 天气: %s(日) %s(夜) | 风力: %s(日) %s(夜)";
                    String row = String.format(template, new String[]{
                            weather.getMonth(),
                            weather.getDay(),
                            weather.getDayAirTemperature(),
                            weather.getNightAirTemperature(),
                            weather.getDayWeather(),
                            weather.getNightWeather(),
                            weather.getDayWindPower(),
                            weather.getNightWindPower()
                    });
                    System.out.println(row);
                }
            }
        }
    }}

代码结果

  7~10日气温过低,多加衣物,以防感冒!

拜拜

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/856679.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号