0
class Celsius {
    public static void main(String[] args){
      celsius(0);
    }
    public static void celsius(double fahrenheitTemperature){
        double celsiusTemperature;
        for (fahrenheitTemperature = 0; fahrenheitTemperature <= 20; fahrenheitTemperature++) {
            celsiusTemperature = ((fahrenheitTemperature - 32) * 5) / 9;
            double celsius_rounded = Math.round(celsiusTemperature * 100)/100.0;
            System.out.println("Fahrenheit: " + fahrenheitTemperature + "  Celsius: " + celsius_rounded);


         }
        }
    }

The code runs fine. The problem is when I call the method, whatever parameter I give, I am getting the same result. If I put 20 or 0 in the parameter of the celsius method, I will get these same results.

What can I do to debug this issue?

My result:

Fahrenheit: 0.0  Celsius: -17.78
Fahrenheit: 1.0  Celsius: -17.22
Fahrenheit: 2.0  Celsius: -16.67
Fahrenheit: 3.0  Celsius: -16.11
Fahrenheit: 4.0  Celsius: -15.56
Fahrenheit: 5.0  Celsius: -15.0
Fahrenheit: 6.0  Celsius: -14.44
Fahrenheit: 7.0  Celsius: -13.89
Fahrenheit: 8.0  Celsius: -13.33
Fahrenheit: 9.0  Celsius: -12.78
Fahrenheit: 10.0  Celsius: -12.22
Fahrenheit: 11.0  Celsius: -11.67
Fahrenheit: 12.0  Celsius: -11.11
Fahrenheit: 13.0  Celsius: -10.56
Fahrenheit: 14.0  Celsius: -10.0
Fahrenheit: 15.0  Celsius: -9.44
Fahrenheit: 16.0  Celsius: -8.89
Fahrenheit: 17.0  Celsius: -8.33
Fahrenheit: 18.0  Celsius: -7.78
Fahrenheit: 19.0  Celsius: -7.22
Fahrenheit: 20.0  Celsius: -6.67
2
  • you are initializing fahrenheitTemperature everytime with 0 in the method for loop that's why. :) Commented Jan 22, 2020 at 3:37
  • Value in the fahrenheitTemperature variable always update the values inside the for loop. Commented Jan 22, 2020 at 3:38

2 Answers 2

2

Your 'FOR' loop initialises fahrenheitTemperature=0, thereby overwriting whatever value you passed in. A solution may be as follow:

for(dobule i = fahrenheitTemperature; i<=20;i++){
Sign up to request clarification or add additional context in comments.

Comments

1

The problem is in the for loop:

 for (fahrenheitTemperature = 0; fahrenheitTemperature <= 20; fahrenheitTemperature++) 

whatever value you pass to the method, you put this value to 0 at the beginning of the for loop.

Change your code with something like this:

public class Celsius {

    public static void main(String[] args) {
        celsius(0);
        celsius(0, 20);
    }

    public static void celsius(double fahrenheitTemperature) {
        double celsiusTemperature = ((fahrenheitTemperature - 32) * 5) / 9;
        double celsius_rounded = Math.round(celsiusTemperature * 100) / 100.0;
        System.out.println("Fahrenheit: " + fahrenheitTemperature + "  Celsius: " + celsius_rounded);

    }

    public static void celsius(double start, double end) {
        for (double fahrenheitTemperature = start; fahrenheitTemperature <= end; fahrenheitTemperature++) {
            celsius(fahrenheitTemperature);

        }
    }
}

In this manner you can print single value or more then one.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.