While in this program I have to enter the temperature again and again in a loop. While I enter the temperature once it shows the correct display, but when I enter the temperature again the program terminates itself.
The code is:
import java.util.Scanner;
public class CheckTemperature
{
public static void main(String [] args)
{
final double TEMPERATURE = 102.5;
double thermostat;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the substance's temperature: ");
thermostat = keyboard.nextDouble();
if(thermostat > TEMPERATURE)
{
System.out.print("The temperature is too high. ");
System.out.print("Turn down the thermostat. ");
System.out.println("Wait for 5 minutes and check the thermostat again. ");
System.out.println("Enter the thermostat here: ");
thermostat = keyboard.nextDouble();
}
else if(thermostat < TEMPERATURE)
{
System.out.println("The temperature is low.");
System.out.println("Turn up the thermostat.");
System.out.println("Check for 5 minutes and check again.");
System.out.println("Enter the thermostat here: ");
thermostat = keyboard.nextDouble();
}
else if(thermostat == TEMPERATURE)
{
System.out.println("The temperature is acceptable. Check again in 15 minutes.");
System.out.println("Enter the thermostat here: ");
thermostat = keyboard.nextDouble();
}
else
{
System.out.println("Enter the correct temperature value ");
System.out.println("Enter the thermostat here: ");
thermostat = keyboard.nextDouble();
}
}
}
This is because your are not in a loop. you ask the temperature once at the top. Then once in a if, then it's end.
public static void main(String [] args)
{
final double TEMPERATURE = 102.5;
double thermostat;
Scanner keyboard = new Scanner(System.in);
while (true) {
System.out.println("Enter the substance's temperature: ");
thermostat = keyboard.nextDouble();
if(thermostat > TEMPERATURE)
{
System.out.print("The temperature is too high. ");
System.out.print("Turn down the thermostat. ");
System.out.println("Wait for 5 minutes and check the thermostat again. ");
}
else if(thermostat < TEMPERATURE)
{
System.out.println("The temperature is low.");
System.out.println("Turn up the thermostat.");
System.out.println("Check for 5 minutes and check again.");
}
else if(thermostat == TEMPERATURE) {
System.out.println("The temperature is acceptable. Check again in 15 minutes.");
}
else{
System.out.println("Enter the correct temperature value ");
}
}
}
Be carefull, the while will never stop, but you can change the condition as you wish.
if you want to get temperature again and again you have to use a loop.
while(true){
if(thermostat > TEMPERATURE)
{
System.out.print("The temperature is too high. ");
System.out.print("Turn down the thermostat. ");
System.out.println("Wait for 5 minutes and check the thermostat again. ");
System.out.println("Enter the thermostat here: ");
thermostat = keyboard.nextDouble();
}
else if(thermostat < TEMPERATURE)
{
System.out.println("The temperature is low.");
System.out.println("Turn up the thermostat.");
System.out.println("Check for 5 minutes and check again.");
System.out.println("Enter the thermostat here: ");
thermostat = keyboard.nextDouble();
}
else if(thermostat == TEMPERATURE) {
System.out.println("The temperature is acceptable. Check again in 15 minutes.");
System.out.println("Enter the thermostat here: ");
thermostat = keyboard.nextDouble();
}
else{
System.out.println("Enter the correct temperature value ");
System.out.println("Enter the thermostat here: ");
thermostat = keyboard.nextDouble();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With