2017-10-15 39 views
-5

当我编译代码时它告诉我我有2个错误,这两个变量可能都没有被初始化错误。变量摄氏和华氏是问题。我相信我已经用他们各自的方法初始化了它们。变量可能未被初始化,即使它已经在方法中

import java.io.*; 

class Converter 
{ 

double celsius,fahrenheit,temperature,inFahrenheit,inCelsius; 

double Celsius (double temperature) 
    { 
    celsius = (5.0/9.0) * (temperature - 32); 
    return celsius; 
    } 
double Fahrenheit (double temperature) 
    { 
    fahrenheit = (9.0/5.0) * temperature + 32; 
    return fahrenheit; 
    } 
} 



public class ConverterTester 
{ 
public static void main(String[] args)throws IOException 
{ 
    double temperature,fahrenheit,celsius; 

    InputStreamReader inStream = new InputStreamReader (System.in); 
    BufferedReader stdin = new BufferedReader (inStream); 

    String intemperature,inCelciusOrFahrenheit; 

    System.out.println("What is the temperature"); 
    intemperature = stdin.readLine(); 
    temperature = Double.parseDouble(intemperature); 

    System.out.println("What is the temperature you wish to convert to, Celsius or Fahrenheit"); 
    inCelciusOrFahrenheit = stdin.readLine(); 


if (inCelciusOrFahrenheit.equals("Celsius")) 
    { 
    Converter Conversion1 = new Converter(); 
    Conversion1.Celsius(celsius); 
    System.out.println("Your new temperature is " + celsius); 
    } 

else if(inCelciusOrFahrenheit.equals("Fahrenheit")) 
    { 
    Converter Conversion2 = new Converter(); 
    Conversion2.Fahrenheit(fahrenheit); 
    System.out.println("Your new temperature is " + fahrenheit); 
    } 
else 
    { 
    System.out.println("Please enter a correct temperature"); 
    System.exit(0); 
    } 
} 
}  

当我打电话的方法摄氏和华氏方法发生错误,我不知道如果我允许我的时候调用的方法使用的变量。然而,我仍然无法找到任何说不允许的事情。

+2

'celcius'和'fahrenheit'都不会被赋值。 –

+2

,你永远不会使用'温度' –

+0

你在哪里初始化'celcius'或'华氏'? –

回答

0

你唯一的问题是你没有初始化温度,摄氏度或华氏温度。
当在这里让您的变量:
double celsius,fahrenheit,temperature,inFahrenheit,inCelsius;,你需要的温度相等的东西,说20
我会建议取出整数摄氏度和华氏度,或将它们设置等于摄氏双打和华氏,那你设置等于温度...数学。

+0

我明白你的观点,但温度需要等于用户输入(在这种情况下,缓冲读取器的标准输入)我只是把这个声明放在错误的地方或完全滥用它?摄氏温度和华氏温度应该是方法,而不是变量。 – Dks1

+0

@ Dks1当然,我只是指出你需要做什么 – 2017-10-15 22:16:39

+0

@ ProgrammingNub好吧谢谢你,我不明白这一点。所以我的声明'温度= Double.ParseDouble(intemperature);'需要在其他地方?或将摄氏和华氏设置为温度? – Dks1