Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reto #43 - Java #5749

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}

}