无法找到呼叫服务类的符号

时间:2014-09-18 16:00:15

标签: java variables find

我为了执行表达式而调用了fahrenheit2Celsius方法,但是它说我的变量无法找到。我似乎无法找到我的错误。我的服务类将编译,但我唯一的错误是编译器无法找到变量" celTemp"。这是我的司机:

    /**
*   TempDriver.java
*   @author Sam Harrison
*   @version 9/17/2014
*/

import java.util.Scanner;
 public class TempDriver
 {
    public static void main(String[] args)
    {
        TempConverter conTemp = new TempConverter();



        double initialTemp, faTemp;

        Scanner scan = new Scanner(System.in);

        System.out.println("MENU");
        System.out.println("0. Quit");
        System.out.println("1. Convert from Fahrenheit to Celsius");
        System.out.println("2. Convert from Celsius to Fahrenheit");
        System.out.println("Which option do you wish to perform?: ");

        int action = scan.nextInt();
        while (action == 1)

        {
            System.out.print("Intitial temperature: ");
                initialTemp = scan.nextDouble();


                conTemp.celsius2Fahrenheit(celTemp, initialTemp);

                System.out.print("The converted temperature is: " + conTemp + " degrees Celsius.");

        }

        action = scan.nextInt();
        while (action == 2)
        {
            System.out.print("Intitial temperature: ");
                initialTemp = scan.nextDouble();


                System.out.print("The converted temperature is: " + conTemp);
        }

        action = scan.nextInt();
        while (action > 2)
        {
            System.out.print("Error: invalid output");
        }


    }

 }

这是我的服务类:

/**
*   TempConverter.java
*   @author Sam Harrison
*   @version 9/17/2014
*/
 public class TempConverter
 {
    public double celsius2Fahrenheit(double celTemp, double initialTemp)
    {

        celTemp = (initialTemp - 32) * 5/9;
        return celTemp;
    }

    public double fahrenheit2Celsius(double faTemp, double initialTemp)
    {
        faTemp = initialTemp * 9/5 + 32;
        return faTemp;
    }
 }

1 个答案:

答案 0 :(得分:0)

您收到此错误,因为celTemp未定义,因此无法找到。首先你必须声明它:

double celTemp = <<some value>>;
conTemp.celsius2Fahrenheit(celTemp, initialTemp);

但是在celsius2Fahrenheit的方法TempConverter中,您定义了double celTemp参数,该函数中未使用该值。真奇怪。 请注意,在Java中没有像C#那样的out参数。我想你真正想做的是:

public double celsius2Fahrenheit(double initialTemp) {
    double celTemp = (initialTemp - 32) * 5/9;
    return celTemp;
}

TempDriver中,您可以将方法称为:

    initialTemp = scan.nextDouble();
    double celTemp = conTemp.celsius2Fahrenheit(initialTemp);

此外,在打印结果时,您使用的conTempTempConverter的实例。这将产生类似TempDriver @ 35c41b的东西,当然你想要使用celTemp