2010-10-23 113 views
1

我必须创建包括雨,雪,云,雾和阳光的自动天气。如何生成自动天气?

根据不同的季节,我需要为所有天气的百分比:预测将在一天内更新的3或4倍。

例如: 冬季|雨:30%雪:30%的阳光:10%雨:10%,雾:20%

我不知道如何实现基于百分比随机状态。一些帮助?

非常感谢和抱歉我的英语不好。

+0

我不明白你的问题到底是什么。你不确定如何分配百分比? – 2010-10-23 13:52:53

+0

那么,“如何将100%随机分为5部分”这个基本问题呢? – Piskvor 2010-10-23 13:54:18

+0

@Pekka 我刚刚改正了百分比,我忘了几个数字 @Piskvor 我认为是这样,问题在于根据“成功”的百分比确定哪个条件。 – Diego 2010-10-23 14:58:57

回答

2

好了,你可以使用:

$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)获得一个随机值和上面的数组来确定天气。

请让我知道这是否适合你。

+0

谢谢你的回复。 问题是我必须实现基于百分比的随机条件,而不是使用api。 我无法理解,一旦我确定了赛季,如何使用百分比来确定条件,直到下一次天气更新。 像这样: in Winter |雨:30%雪:30%晴朗:10%多云:10%,雾:20% 秋季|雨:20%雪:0%晴朗:30%云:40%,雾:10% 使用这些参数,脚本应该会产生天气状况。 – Diego 2010-10-23 15:04:57

+0

哦,基本上,你有30%的机会下雨,30%的机会下雪,10%的机会,太阳,10%的机会和20%的机会, – Claudiu 2010-10-23 15:08:49

+0

@Claudio正好。 – Diego 2010-10-23 15:11:22

2

大答案由克劳迪乌但如果你想查看与华氏(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 . "&deg; F"; 
?>