I get an error message on my code below. How can I fix it?

import java.util.Scanner;

public class CelsiusToFahrenheit {


public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);

double tempF;
double tempC;

System.out.println("Enter temperature in Celsius: ");
tempC = scnr.nextDouble();

System.out.print("Fahrenheit: ");
System.out.println(tempF);
return;
}
}


CelsiusToFahrenheit.java:16: error: variable tempF might not have been initialized
System.out.println(tempF);
^
1 error

Respuesta :

Answer:

tempF = (tempC * 1.8) + 32;

Explanation:

Required

Correct the error in the program

The error is due to tempF not been initialized or being calculated

Correct the error by writing the formula for tempF before tempF was printed

tempF = (tempC * 1.8) + 32;

Writing the formula ensures that tempF is initialized with the expected equivalent value for tempC