If ... elseif error when re-entering values

In this program, I need to enter the temperature over and over again in the loop. As long as I enter the temperature, as soon as it shows the correct display, but when I enter the temperature again, the program ends.

Code:

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 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();
        }
    }
}

      

+3


source to share


4 answers


This is because you are not in a loop. you ask for the temperature once upstairs. Then once in the if, then it ends.

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 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 careful, time will never stop, but you can change the condition as you see fit.

+5


source


if you want to get the temperature over and over, you need to use a cycle.



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();
        }
    }

      

+4


source


Your code is not built to repeat any AKA task. Add a loop to your code so that it repeats the action as desired.

Here we are using while (1==1)

to create an endless loop. In addition, we add exit criteria when checking for input == "X", which starts System.exit

out of the loop. This helps maintain a graceful exit from the application without closing it.

    final double TEMPERATURE = 102.5;
    double thermostat;
    while(true){
           Scanner keyboard = new Scanner(System.in);
           System.out.println("Enter the substance temperature or press X to exit: ");
           String input = keyboard.next();
           if(input.equals("X")){
              System.exit(0);
           } else{
              thermostat = Double.parseDouble(input);   
              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 ");
                }
           }
    }

      

+1


source


Murtaza. You can just use a loop or use any recursive method and call it in your main method. And the error you are talking about DOUBLE CANNOT BE FULFILLED due to the fact that there is no such method as equals () in the Scanner class. Also, you shouldn't be comparing an int value with a double type variable. Again, to compare it inside an if-else block, you must use the equals () method found in Double, the wrapper class, to pass the object as its parameter.

0


source







All Articles