第十六次java作业天气预报

目录

5-1开通阿里云市场天气预报服务

5-2实现24小时天气预报查询功能

5-3格式化输出天气预报结果

5-4实现未来3天天气预报查询功能

5-5实现未来7天天气预报查询功能

 Java代码总结


5-1开通阿里云市场天气预报服务

天气 API包含丰富的天气数据,不仅有实时温度、湿度、风速、降水等这些基础数据,还有过去的历史天气数据和未来的天气预测数据,顶级的天气 API 接口还会包括天气灾害预警,空气质量指数,日出日落、潮汐及月相相关的天文气象等数据。 

     操作系统已经为我们实现了很多功能,它们都被封装成了一个一个的函数,有成百上千个之多,这些函数就叫做 API。程序员要想使用某个功能,只需要调用相应的函数。Windows、Linux、Mac OS、Unix 这些常见的操作系统大部分功能都使用C语言开发,它们的 API 也以C语言的形式呈现。操作系统 API 数目众多,官方必须提供详细的说明文档(Windows API 的说明文档叫 MSDN),程序员在使用 API 时,需要频繁地查阅这些文档。 

  访问阿里云

     说得更加通俗易懂一些,别人写好的代码,或者编译好的程序,提供给你使用,就叫做API。你使用了别人代码(或者程序)中的某个函数、类、对象,就叫做使用了某个API。 

 

5-2实现24小时天气预报查询功能

代码

结果

 Java包里的内容

 

 源代码Java包

 

5-3格式化输出天气预报结果

格式化 

5-4实现未来3天天气预报查询功能

代码

结果 

5-5实现未来7天天气预报查询功能

代码

 结果

 Java代码总结

package imoocweather;  //包名
import com.imooc.weather.DayWeather;
import com.imooc.weather.HourWeather;
import com.imooc.weather.WeatherUtils;
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<HourWeather> weatherList = weatherUtils.w24h("811c218772da47f28834cc6a6a50ce41",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<DayWeather> weatherList = weatherUtils.w3d("811c218772da47f28834cc6a6a50ce41", 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<DayWeather> weatherList = weatherUtils.w7d("811c218772da47f28834cc6a6a50ce41", city);//输入自己的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.getNightWindPower()
                            });
                            System.out.println(row);
                        }
                    }
                }
    }}

 运行结果在上面

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值