Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if...elseif error while entering values again and again

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();
        }
    }
}
like image 876
Murtaza Vora Avatar asked Aug 23 '26 14:08

Murtaza Vora


2 Answers

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.

like image 177
Anthony Raymond Avatar answered Aug 26 '26 03:08

Anthony Raymond


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();
        }
    }
like image 27
sushi Avatar answered Aug 26 '26 04:08

sushi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!