Error: celsius cannot be resolved to a variable

故事扮演 提交于 2019-12-24 15:14:48

问题


Getting error with the following code:

import java.util.Scanner;
public class FahrenheitToCelsius{


 public static void main(String[]args){
    convertFToC();
 }

  /*gets input representing fahrenheit and displays celsius equivalent*/
  public static void convertFToC(){
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter Fahrenheit temperature");
    double fahrenheit = scan.nextInt();            
    System.out.println(fahrenheit + " degrees Fahrenheit is " +
      fMethod(celsius) + " degrees Celsius");
  }


  /* calculates and returns the celsius equivalent */
  public static double toCelsius(double fahr){
    int BASE = 32;
    double CONVERSION_FACTOR = 9.0/ 5.0;
    double celsius = ((fahr-BASE)/(CONVERSION_FACTOR));
    return celsius;
  }
}

I get the following:

Error: celsius cannot be resolved to a variable

I need to use fMethod to call the toCelsius in the System.out.println however i keep getting this error.


回答1:


System.out.println(fahrenheit + " degrees Fahrenheit is " + fMethod(celsius) + " degrees Celsius"); //Step 4

should probably read

System.out.println(fahrenheit + " degrees Fahrenheit is " + toCelsius(fahrenheit) + " degrees Celsius"); //Step 4



回答2:


You haven't shown fMethod at all, but it looks like you just want:

System.out.println(fahrenheit + " degrees Fahrenheit is " + toCelsius(fahrenheit)
                   + " degrees Celsius");

You can't use celsius in main because it's a local variable in the toCelsius method. Instead, you need to call that method and use the return value.




回答3:


You call fMethod which doesn't exist and pass the value of celsius when I think you meant fahrenheit.

The variable celsius is local to toCelsius() and is therefore not resolvable inside convertFToC().




回答4:


You need to call your toCelsius(double fahr) method, like this:

System.out.println(fahrenheit + " degrees Fahrenheit is " + toCelsius(fahrenheit) + " degrees Celsius"); // Step 4



回答5:


in the method convertFToC()

You get the fahrenheit from the user and call fMethod(celsius) You were supposed to call fMethod(farenheit)

More important:

If you read the compiler error message, you get not just the error, but the file name and the line number. If you go to the line number, you see a celcius variable out of nowhere.

This is easy. I know asking on SO is easier, but you will never learn to read error messages and solve problems this way.

Learn to read and understand error messages. They are there for a reason.




回答6:


On a line marked step4 you have an undeclared variable named celsius. Should be fahrenheit perhaps?




回答7:


You are using celsius here:

System.out.println(fahrenheit + " degrees Fahrenheit is " + fMethod(celsius) + " degrees Celsius");

However, you did declare celsius only in toCelsius and that's out of scope.



来源:https://stackoverflow.com/questions/6806568/error-celsius-cannot-be-resolved-to-a-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!