Code not running ,I need help to find problem in code

45 views Asked by At

I need find the error .

the output must to "hot" or "cold" .

the compile it appears that there are curly brackets missing .

like these : error: missing return statement } ^

public class quiz{

    public static String celsius (double F){

        double C = 5.0 / 9.0 *(F - 32) ;

        if(C > 30)
            return "hot";

        else if(1 < 10)
            return "cold";


        }//end method

    public static void main(String args[]){

        System.out.println(celsius(70.0));
        System.out.println(celsius(14.5));



    }//end main
}//end class
3

There are 3 answers

0
jwenting On

What if the temperature is higher than 10 but lower than 30? What does it return then?

Answer that question and you know why you get the compiler error you're getting.

0
Mr. Polywhirl On

Your issue is that you are not returning either in an else or at the very end.

Also, please adhere to the Java naming conventions.

public class Quiz {
    public static String calculateCelsius(double fahrenheit) {
        double celcius = 5.0 / 9.0 * (fahrenheit - 32);

        if (celcius > 30)
            return "hot";
        else if (celcius < 10) // I think you want to compare celsius, not 1
            return "cold";
        else
            return "acceptable"; // You need an else,

        // ...or you need to return at the very end.
        // return "acceptable";
    }

    public static void main(String[] args) {
        System.out.println(calculateCelsius(70.0)); // acceptable
        System.out.println(calculateCelsius(14.5)); // cold
    }
}

I would break this into two separate functions: one that converts temperature, and another that takes the converted temperature and priduces a string.

public class Quiz {
    public static double calculateCelsius(double fahrenheit) {
        return 5.0 / 9.0 * (fahrenheit - 32);
    }

    public static String determineClimate(double temperatureInCelcius) {
        if (temperatureInCelcius > 30)
            return "hot";
        else if (temperatureInCelcius < 10) // I think you want to compare celsius, not 1
            return "cold";
        else
            return "acceptable"; // You need an else,

        // ...or you need to return at the very end.
        // return "acceptable";
    }

    public static void main(String[] args) {
        System.out.println(determineClimate(calculateCelsius(70.0))); // acceptable
        System.out.println(determineClimate(calculateCelsius(14.5))); // cold
    }
}
0
Tripple1ZRO On

here is the corrected code that should work

public class Quiz {
    public static String celsius(double F) {
        String hot = "hot";
        String cold = "cold";
        double C = 5.0 / 9.0 * (F - 32);

        if (C > 30) {
            return hot;
        } else if (C < 10) {
            return cold;
        } else {
            return "Neither hot nor cold";
        }
    }
        public static void main(String args[]) {
        System.out.println(celsius(70.0));
        System.out.println(celsius(14.5));
    }
}