-1

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
  • 1
    While *we* know that `1 < 10` is always true, I wouldn't expect the compiler to know that and use it to determine reachability. Why do you even if that condition of `if (1 < 10)`? Don't you *always* want to return `"cold"` if you've already decided not to return "hot"? You could just have `else return "cold";` or remove the "else" (and condition) entirely. (Or better, use a conditional, but that's a larger change...) – Jon Skeet Apr 05 '23 at 14:16
  • 2
    Admittedly I'd also say it's an odd design for a method called `celsius` to return a string value, and for that value to just be "hot" or "cold", but that's a different matter... – Jon Skeet Apr 05 '23 at 14:18
  • 1
    Poor title. Rewrite to summarize your specific technical issue. – Basil Bourque Apr 05 '23 at 19:33

3 Answers3

1

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.

jwenting
  • 5,505
  • 2
  • 25
  • 30
0

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
    }
}
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
0

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