我必须创造一个自动的天气,包括雨,雪,云,雾和晴朗。
根据季节的不同,我需要为所有天气设置一个百分比:天气预报将在一天内更新3到4次。
示例:冬季|雨: 30%雪: 30%晴天: 10%多云: 10%,雾: 20%
我不知道如何实现基于百分比的随机条件。帮帮忙?
对于我糟糕的英语,非常感谢和抱歉。
发布于 2010-10-23 21:54:06
嗯,你可以使用:
$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,看看您有哪些可用的数据。
编辑
我不明白行动组第一次想要什么。基本上,它甚至更容易。
$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)
获取随机值,使用上面的数组确定天气。
请让我知道这对你是否有效。
发布于 2015-10-15 17:22:48
Claudiu回答得很好,但如果你想看看下面这个可能的例子:
<?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";
?>
https://stackoverflow.com/questions/4004285
复制相似问题