错误变量可能未初始化
问题描述:
出于某种原因,我得到以下列出的这两个错误。错误变量可能未初始化
Error variable fahrenhuit might not have been initialized.
Error variable kelvin might not have been initialized.
这是我的代码。
import java.util.*;
public class TemperatureConversion
{
public static void main (String [] args)
{
Scanner sc = new Scanner(System.in);
double temperature = sc.nextDouble();
double kelvin = kelvinCalculations(kelvin);
double fahrenhuit = fahrenhuitCalculations(fahrenhuit);
System.out.println(temperature + "Degrees Celsius is equivalent to " + kelvin + " Kelvin and " + fahrenhuit + " Degrees Fahrenhuit");
}
public static double kelvinCalculations(double temperature)
{
double kelvin = temperature + 273.15;
return kelvin;
}
public static double fahrenhuitCalculations(double temperature)
{
double fahrenhuit = (((temperature * 9)/5) + 32);
return fahrenhuit;
}
}
此程序从摄氏和fahrenhuit和输入转换/输出必须在主。这就是我认为导致我的大部分问题......也就是在子模块之间传递变量。
答
调用代码应该是这样的:
double kelvin = kelvinCalculations(temperature);
double fahrenhuit = fahrenhuitCalculations(temperature);
您正在使用的变量他们在初始化之前。你的意思是'开尔文计算(温度)'? – chrylis
在这两种方法调用中,你使用的是错误的变量......正确的是在两种情况下,温度变量 – mayha
在使用它们之前引入你的变量:'double kelvin = 0;双fahrenhuit = 0;' – DimaSan