目录
开通阿里云市场天气预报服务
Java包里的内容
实现24小时天气预报查询功能
查询未来3天天气预报
Java代码天气预报
开通阿里云市场天气预报服务
Java包里的内容
实现24小时天气预报查询功能
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 Weather { //创建类
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, (Object[]) (Object) 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());
运行结果
查询未来3天天气预报
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);
}
}
}
}}