duanliujie8639 2010-10-23 13:50
浏览 46
已采纳

如何生成自动天气?

I have to create an automatic weather including rain, snow, clouds, fog and sunny.

Depending on the season I need to set a percentage for all weather: the forecast will be updated 3 or 4 times during a day.

Example: Winter | Rain: 30% Snow: 30% Sunny: 10% Cloudy: 10%, Fog: 20%

I do not know how to implement a random condition based on percentages. Some help?

Many thanks and sorry for my bad English.

  • 写回答

2条回答 默认 最新

  • dongyu4863 2010-10-23 13:54
    关注

    Well, you can use:

    $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"; 
    

    Just take a look on the XML and see what data you have available.

    EDIT

    I didn't understand what the OP wanted the first time. Basically, it's even easier.

    $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;
    

    What I suggest tough, is to keep your values in arrays (for example the seasons) as well as the values for chances to have one type of weather or another.

    array (
       [winter] => array (
           [30] => 'Rainy',
           [60] => 'Snowy',
           ... // the other chances of weather
       ),
       [spring] => array (
           ...
       ), 
       ... // and so on
    )
    

    Use mt_rand(0,100) to get a random value and the array above to determine the weather.

    Please let me know if this works for you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀