2016-08-23 157 views
-2

出於某種原因,我得到以下列出的這兩個錯誤。錯誤變量可能未初始化

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和輸入轉換/輸出必須在主。這就是我認爲導致我的大部分問題......也就是在子模塊之間傳遞變量。

+1

您正在使用的變量他們在初始化之前。你的意思是'開爾文計算(溫度)'? – chrylis

+0

在這兩種方法調用中,你使用的是錯誤的變量......正確的是在兩種情況下,溫度變量 – mayha

+0

在使用它們之前引入你的變量:'double kelvin = 0;雙fahr​​enhuit = 0;' – DimaSan

回答

4

調用代碼應該是這樣的:

double kelvin = kelvinCalculations(temperature); 
double fahrenhuit = fahrenhuitCalculations(temperature);