如何生成自动天气?

发布于 2024-09-28 14:34:12 字数 187 浏览 1 评论 0原文

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

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

例子: 冬天 |雨:30% 雪:30% 晴天:10% 阴天:10%,雾:20%

我不知道如何根据百分比实现随机条件。有帮助吗?

非常感谢,并对我糟糕的英语感到抱歉。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独夜无伴 2024-10-05 14:34:12

那么,您可以使用:

$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,看看您有哪些可用数据。

编辑

我第一次不明白OP想要什么。基本上,这更容易。

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

我建议的困难是将您的值保存在数组中(例如季节)以及出现一种或另一种天气的机会值。

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

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

请告诉我这是否适合您。

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.

鹿港小镇 2024-10-05 14:34:12

Claudiu 的回答很好,但如果您想用华氏度 (F) 查看可能的示例,如下所示:

 <?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 . "° F";
 ?>

Great answer by Claudiu but if you want to view with Fahrenheit (F) that possible example Below:

 <?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 . "° F";
 ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文