forked from mouredev/retos-programacion-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
Retos/Reto #43 - SIMULADOR DE CLIMA [Fácil]/java/asjordi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package facil.reto43; | ||
|
||
public class WeatherSimulator { | ||
|
||
public static void main(String[] args) { | ||
simulate(7, 25, 0.2); | ||
} | ||
|
||
public static void simulate(int days, double initialTemp, double initialRainProbability){ | ||
double temperature = initialTemp; | ||
double chanceOfRain = initialRainProbability; | ||
double tempMax = temperature; | ||
double tempMin = temperature; | ||
int rainDays = 0; | ||
|
||
for (int i = 1; i <= days ; i++) { | ||
// simulate change of temperature | ||
if (Math.random() < 0.1){ | ||
temperature += Math.random() < 0.5 ? 2 : -2; | ||
} | ||
|
||
// update rain probability | ||
if (temperature > 25) chanceOfRain += 0.2; | ||
else if (temperature < 5) chanceOfRain -= 0.2; | ||
|
||
// set limite to rain probability | ||
chanceOfRain = Math.min(1, Math.max(0, chanceOfRain)); | ||
|
||
// simulate rain | ||
if (Math.random() < chanceOfRain){ | ||
temperature -= 1; | ||
rainDays++; | ||
} | ||
|
||
// update math and min temperature | ||
tempMax = Math.max(tempMax, temperature); | ||
tempMin = Math.min(tempMin, temperature); | ||
} | ||
|
||
String data = String.format("Max temperature %s °C%nMin temperature %s °C%nRaining days %s", tempMax, tempMin, rainDays); | ||
|
||
System.out.println(data); | ||
} | ||
|
||
} |