C#如何實(shí)現(xiàn)天氣預(yù)報(bào)功能?

這篇文章將為大家詳細(xì)講解有關(guān)C#如何實(shí)現(xiàn)天氣預(yù)報(bào)功能?,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

10年的勐海網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。全網(wǎng)營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整勐海建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)公司從事“勐海網(wǎng)站設(shè)計(jì)”,“勐海網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。

引用部分

由于本次是控制臺(tái)應(yīng)用,就沒有頁面設(shè)計(jì)了。在VS中新建控制臺(tái)程序后,右擊“引用”——“添加服務(wù)引用”。

在“添加服務(wù)引用”左下角選擇“高級(jí)”。

C#如何實(shí)現(xiàn)天氣預(yù)報(bào)功能?

在“服務(wù)引用設(shè)置中”選擇左下角的“添加web引用”。

C#如何實(shí)現(xiàn)天氣預(yù)報(bào)功能?

在其中輸入天氣預(yù)報(bào)提取網(wǎng)址的url:

http://www.webxml.com.cn/WebServices/WeatherWebService.asmx

C#如何實(shí)現(xiàn)天氣預(yù)報(bào)功能?

至此,引用功能就已完成。該網(wǎng)站提供了很多查詢方法,此處我們使用的是getWeatherCityName(),方法詳細(xì)內(nèi)容如下:

C#如何實(shí)現(xiàn)天氣預(yù)報(bào)功能?

當(dāng)然,你也可以轉(zhuǎn)到該url查看更多定義,選擇適合你的方法。

代碼實(shí)現(xiàn)部分

Main方法里直接引用:

 WeatherWebService myweather = new WeatherWebService();
      string[] myweathers = myweather.getWeatherbyCityName("鄭州");
      for (int i = 0; i < myweathers.Length;i++ )
      {  Console.WriteLine(myweathers[i]);  }

傳入的值盡量不要帶“市”,返回的數(shù)組循環(huán)輸出后結(jié)果如下:

C#如何實(shí)現(xiàn)天氣預(yù)報(bào)功能?

其中各項(xiàng)所代表的含義可以查看上方官網(wǎng)的說明,這里為了讓布局好看一點(diǎn),讓重點(diǎn)突出一點(diǎn),可以使用到控制臺(tái)的字體顏色轉(zhuǎn)換語句:

Console.ForegroundColor = ConsoleColor.顏色;

完善后的代碼如下:

 WeatherWebService myweather = new WeatherWebService();
      string[] myweathers = myweather.getWeatherbyCityName("鄭州");

      Console.ForegroundColor = ConsoleColor.Red;
      Console.WriteLine("今日天氣:\n更新時(shí)間:" + myweathers[4]);
      Console.WriteLine("當(dāng)前選擇地區(qū):" + myweathers[0] + "_" + myweathers[1] + "\n");
      Console.ForegroundColor = ConsoleColor.White;

      Console.Write(myweathers[6] + "(今日) 風(fēng)向&風(fēng)力:" + myweathers[7]);
      Console.ForegroundColor = ConsoleColor.Green;
      Console.WriteLine(" 氣溫:" + myweathers[5] + "\n");
      Console.ForegroundColor = ConsoleColor.White;

      Console.WriteLine("當(dāng)前實(shí)況(數(shù)據(jù)每2.5小時(shí)左右自動(dòng)更新一次):\n" + myweathers[10] + myweathers[11]);

      Console.Write(myweathers[13] + "(明天) 風(fēng)向&風(fēng)力:" + myweathers[14]);
      Console.ForegroundColor = ConsoleColor.Green;
      Console.WriteLine(" 氣溫:" + myweathers[12] + "\n");
      Console.ForegroundColor = ConsoleColor.White;

      Console.Write(myweathers[18] + "(后天) 風(fēng)向&風(fēng)力:" + myweathers[19]);
      Console.ForegroundColor = ConsoleColor.Green;
      Console.WriteLine(" 氣溫:" + myweathers[17] + "\n");
      Console.ForegroundColor = ConsoleColor.White;

當(dāng)然,你也可以在后面加一個(gè)判斷,輸入1的話可以查詢其他城市,然后獲取輸入的值,傳入方法中。如果感興趣的話可以看一下源碼,這里就不再過多展示。

運(yùn)行效果

C#如何實(shí)現(xiàn)天氣預(yù)報(bào)功能?

關(guān)于C#如何實(shí)現(xiàn)天氣預(yù)報(bào)功能?就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

名稱欄目:C#如何實(shí)現(xiàn)天氣預(yù)報(bào)功能?
網(wǎng)址分享:http://bm7419.com/article32/jjcpsc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、品牌網(wǎng)站設(shè)計(jì)網(wǎng)站內(nèi)鏈、全網(wǎng)營銷推廣微信公眾號(hào)、網(wǎng)站設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計(jì)