Skip to content

Commit 39fb052

Browse files
committed
feat: Add user input and real-time delay to simulate dynamic cooling
🧠 Logic: - User inputs starting temperature and cooling rate. - `Thread.sleep(1000)` simulates 1-second delay between each cycle. - Uses `continue` if temperature is above 40°C to skip thermometer output. - Once temperature <= 0°C, prints "We have ice!" and exits using `break`. 🎯 Focus: Enhances realism and interactivity of the cooling simulation. Signed-off-by: Somesh diwan <[email protected]>
1 parent 46332c3 commit 39fb052

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.util.Scanner;
2+
3+
public class IceFormationUsingThread {
4+
public static void main(String[] args) throws InterruptedException {
5+
Scanner scanner = new Scanner(System.in);
6+
7+
System.out.println("🌡️ Breaking the Ice - Cooling Simulation");
8+
System.out.print("Enter starting temperature (°C): ");
9+
int temperature = scanner.nextInt();
10+
11+
System.out.print("Enter cooling rate per cycle (°C): ");
12+
int decrement = scanner.nextInt();
13+
14+
boolean machineOn = true;
15+
16+
System.out.println("\nStarting cooling process...\n");
17+
18+
while (machineOn) {
19+
Thread.sleep(1000); // Delay to simulate real-time cooling
20+
21+
temperature -= decrement;
22+
23+
if (temperature > 40) {
24+
System.out.println("Too hot! Thermometer unusable.");
25+
continue;
26+
}
27+
28+
System.out.println("Temperature -> " + temperature + "°C");
29+
30+
if (temperature <= 0) {
31+
System.out.println("❄️ We have ice!");
32+
break;
33+
}
34+
}
35+
36+
scanner.close();
37+
}
38+
}

0 commit comments

Comments
 (0)