源代码
链接:https://pan.baidu.com/s/1JpaazJCJXBktuFxeOhKsUw
提取码:1234
代码来自课程,仅供学习使用。
侵删
```java
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 enter = new Scanner(System.in); // 输入
int i = enter.nextInt();
System.out.println("用户输入数字: " + i);
if (i == 1){
System.out.print("请输入城市名称查询未来24小时天气预报: ");
String city = enter.next();
WeatherUtils weatherUtils = new WeatherUtilsImpl();
List<HourWeather> weatherList = weatherUtils.w24h("ce0a9b9e0b9b4412b89470c8f4a4b6fe", city);
// System.out.println(weatherList);
if (weatherList.size() == 0) {
System.out.println("没有查询到城市天气数据: ");
}
else {
for (HourWeather hourWeather : weatherList) {
String temple = "%s 月 %s 日 %s 时 | %-3s | %-20s | %-8s | %-4s ℃"; // - 代表左对齐
String row = String.format(temple, new String[]{
hourWeather.getMonth(),
hourWeather.getDay(),
hourWeather.getHour(),
hourWeather.getWindDirection(),
hourWeather.getWindPower(),
hourWeather.getWeather(),
hourWeather.getTemperature()
});
System.out.println(row);
}
}
}
if (i == 2) {
System.out.print("请输入城市名称查询未来3天天气预报: ");
String city = enter.next();
WeatherUtils weatherUtils = new WeatherUtilsImpl();
List<DayWeather> weatherList = weatherUtils.w3d("ce0a9b9e0b9b4412b89470c8f4a4b6fe", city);
// System.out.println(weatherList);
if (weatherList.size() == 0) {
System.out.println("没有查询到城市天气数据");
}
else {
for (DayWeather weather: weatherList){
String temple = "%-2s 月 %-2s 日 | 气温: %s℃(日) %s℃(夜) | 天气: %s(日) %s(夜) | 风力: %s(日) %s(夜)";
String row = String.format(temple, new String[] {
weather.getMonth(),
weather.getDay(),
weather.getDayAirTemperature(),
weather.getNightAirTemperature(),
weather.getDayWeather(),
weather.getNightWeather(),
weather.getDayWindPower(),
weather.getNightWindPower()
});
System.out.println(row);
}
}
}
if (i == 3) {
System.out.print("请输入城市名称查询未来24小时天气预报: ");
String city = enter.next();
WeatherUtils weatherUtils = new WeatherUtilsImpl();
List<DayWeather> weatherList = weatherUtils.w7d("ce0a9b9e0b9b4412b89470c8f4a4b6fe", city);
// System.out.println(weatherList);
if(weatherList.size() == 0) {
System.out.println("没有查询到城市天气数据");
}
else {
for (DayWeather weather : weatherList) {
String temple = "%-2s 月 %-2s 日 | 气温: %s℃(日) %s℃(夜) | 天气: %s(日) %s(夜) | 风力: %s(日) %s(夜)";
String row = String.format(temple, new String[] {
weather.getMonth(),
weather.getDay(),
weather.getDayAirTemperature(),
weather.getNightAirTemperature(),
weather.getDayWeather(),
weather.getNightWeather(),
weather.getDayWindPower(),
weather.getNightWindPower()
});
System.out.println(row);
}
}
}
System.out.println("查询结束");
}
}