首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何生成自动天气?

如何生成自动天气?
EN

Stack Overflow用户
提问于 2010-10-23 21:50:45
回答 2查看 439关注 0票数 1

我必须创造一个自动的天气,包括雨,雪,云,雾和晴朗。

根据季节的不同,我需要为所有天气设置一个百分比:天气预报将在一天内更新3到4次。

示例:冬季|雨: 30%雪: 30%晴天: 10%多云: 10%,雾: 20%

我不知道如何实现基于百分比的随机条件。帮帮忙?

对于我糟糕的英语,非常感谢和抱歉。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-10-23 21:54:06

嗯,你可以使用:

代码语言:javascript
运行
复制
$location = 'Rome';
$document = file_get_contents(str_replace(" ", "+", "http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=".$location));
$xml = new SimpleXMLElement($document); 
echo "$location: ".$xml->temp_c."° C"; 

只需看一看XML,看看您有哪些可用的数据。

编辑

我不明白行动组第一次想要什么。基本上,它甚至更容易。

代码语言:javascript
运行
复制
$weather = mt_rand(0,100);
$season = 'winter';
switch($season) {
    case 'winter': {
        if ($weather < 30) {
            $output = 'Rainy';
        } else if ($weather >=30 && $weather < 60) {
            $output = 'Snowy';
        }
        // goes on on the same ideea of testing the value of $weather
        break;
    }
    // other seasons 
} 

echo $output;

我建议的难点是将您的值保存在数组中(例如季节),以及具有一种或另一种天气类型的机会的值。

代码语言:javascript
运行
复制
array (
   [winter] => array (
       [30] => 'Rainy',
       [60] => 'Snowy',
       ... // the other chances of weather
   ),
   [spring] => array (
       ...
   ), 
   ... // and so on
)

使用mt_rand(0,100)获取随机值,使用上面的数组确定天气。

请让我知道这对你是否有效。

票数 2
EN

Stack Overflow用户

发布于 2015-10-15 17:22:48

Claudiu回答得很好,但如果你想看看下面这个可能的例子:

代码语言:javascript
运行
复制
 <?php
 $location = 'Washington';
 $document = file_get_contents(str_replace(" ", "+", "http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=" . $location));
 $xml = new SimpleXMLElement($document);
 echo $xml->temp_f . "&deg; F";
 ?>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4004285

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档