C# 天气预报

时间:2023-03-09 03:31:46
C# 天气预报

问题描述:

使用C#做一个简易的天气预报系统

问题解决:

主要使用类如下:

WeatherLoc:包含常用的调用中国气象局天气情况接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WeatherReport.ServiceReference1; namespace WeatherReport
{
class WeatherLoc
{
//调用天气接口的类

private static WeatherWebServiceSoapClient ws= new WeatherWebServiceSoapClient("WeatherWebServiceSoap"

);

        public WeatherLoc()
{
}
     //查看天气接口支持的省份
public String[] GetSupportProvince()
{
String[] supportProv = ws.getSupportProvince();
return supportProv;
}
    //查看天气情况支持的城市
public String[] GetSupportCity(String province)
{
return ws.getSupportCity(province);
}
     //查看指定城市的天气信息
public WeatherInfo GetWeatherInfo(String city)
{
String[] allInfo = ws.getWeatherbyCityName(city);
WeatherInfo weather = new WeatherInfo();
weather.Province = allInfo[];
weather.CityName = allInfo[];
weather.LastUpdateTime = allInfo[];
weather.Temperature = allInfo[];
weather.WeatherLive = allInfo[];
weather.LivingIndex = allInfo[];
weather.CityInfo = allInfo[]; FutureInfo[] future = new FutureInfo[]; //今天天气信息
future[].temp = allInfo[];
future[].overView = allInfo[];
future[].windDirection = allInfo[];
future[].tendencyBeg = allInfo[];
future[].tendencyEnd = allInfo[]; //明天的天气信息
future[].temp = allInfo[];
future[].overView = allInfo[];
future[].windDirection = allInfo[];
future[].tendencyBeg = allInfo[];
future[].tendencyEnd = allInfo[]; //后天的天气信息
future[].temp = allInfo[];
future[].overView = allInfo[];
future[].windDirection = allInfo[];
future[].tendencyBeg = allInfo[];
future[].tendencyEnd = allInfo[]; weather.FutureInfo = future; return weather;
}
}
}

WeatherInfo类:包含查询天气情况信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace WeatherReport
{
//未来三天天气情况结构体,C#中结构体默认访问权限是private,C++结构体默认访问权限为public
struct FutureInfo
{
public String temp; //温度
public String overView; //概况
public String windDirection; //风向和风力
public String tendencyBeg; //趋势开始图片
public String tendencyEnd; //趋势结束图片
}; class WeatherInfo
{
private String province; //省份
private String cityName; //城市名称
private String lastUpdateTime; //最后更新时间
private String temperature; //温度
private FutureInfo[] info; //未来三天天气情况
private String weatherLive; //现在天气实况
private String livingIndex; //生活指数
private String cityInfo; //城市介绍 public WeatherInfo()
{
cityName = "";
lastUpdateTime = "";
temperature = "";
info = new FutureInfo[];
weatherLive = "";
livingIndex = "";
cityInfo = "";
} public String Province
{
get
{
return province;
}
set
{
province = value;
}
} public FutureInfo[] FutureInfo
{
get
{
return info;
}
set
{
info = value;
}
} public String CityName
{
get
{
return cityName;
} set
{
cityName = value;
}
} public String LastUpdateTime
{
get
{
return lastUpdateTime;
}
set
{
lastUpdateTime = value;
}
} public String Temperature
{
get
{
return temperature;
}
set
{
temperature = value;
}
} public String WeatherLive
{
get
{
return weatherLive;
}
set
{
weatherLive = value;
}
} public String LivingIndex
{
get
{
return livingIndex;
}
set
{
livingIndex = value;
}
} public String CityInfo
{
get
{
return cityInfo;
}
set
{
cityInfo = value;
}
}
}
}

具体使用中国气象局天气接口方法如下:

(1)添加服务引用

C# 天气预报

(2)包含服务器引用接口

C# 天气预报

(3)接口使用,参考WeatherLoc类中接口调用

执行效果:

C# 天气预报

C# 天气预报