Skip to content

Commit

Permalink
Reto mouredev#43 - Java
Browse files Browse the repository at this point in the history
  • Loading branch information
ASJordi committed Nov 14, 2023
1 parent b584c3b commit 2e92820
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions Retos/Reto #43 - SIMULADOR DE CLIMA [Fácil]/java/asjordi.java
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);
}

}

0 comments on commit 2e92820

Please sign in to comment.