0

これまでにこの問題が発生したことはありません。なぜこれが行われているのかわかりません。method.calcTonsCO2(); を呼び出そうとしています。しかし、シンボルエラーが見つかりません。メソッドが存在することは知っており、タイプミスはしていません...どうしたの

メイン メソッド テスター:

public class CO2FootprintV1Tester
{
    public static void main (String[] args)
    {
        //declaration of variables
        double gals, tons, pounds;

        //initialization        
        CO2FootprintV1Tester footprint = new CO2FootprintV1Tester();

        //methods
        footprint.calcTonsCO2();
        footprint5.convertTonsToPoundsCO2();
        tons = footprint.getTonsCO2();
        pounds = footprint.getPoundsCO2();
    }
}

メイン メソッド クラス:

public class CO2FootprintV1
{   
    //declaration of private instance variables
    private double myGallonsUsed; 
    private double myTonsCO2;
    private double myPoundsCO2;

    /**
     * Constructor for ojbects of type CO2FootPrintV1
     * @param gals are gallons used
     * 
     */

    CO2FootprintV1 (double gals)
    {        
        myGallonsUsed = gals;
    }

    /**
     *  Method to calculate tons of CO2
     */
    public void calcTonsCO2()
    {
        myTonsCO2 = (8.78 * Math.pow(10 , -3)) * myGallonsUsed;        
    }

    /**
     * method to convert TOns to Pounds
     */
    public void convertTonsToPoundsCO2()
    {
        myPoundsCO2 = myGallonsUsed * 2204.62;
    }

    /**
     * Method to get the MyTonsCO2 private instance
     */
    public double getTonsCO2()
    {
        return myTonsCO2;
    }

    /**
     * Method to get the MyPoundsCO2 private instance
     */
    public double getPoundsCO2()
    {
        return myPoundsCO2;
    }
}
4

1 に答える 1

1

使用している特定のプログラミング言語はよくわかりませんが、calcTonsCO2()メソッドはクラスで定義されてCO2FootprintV1いますが、クラスのインスタンスでそれを呼び出そうとしていますCO2FootprintV1Tester

public class CO2FootprintV1
 {
    .......
public void calcTonsCO2()
{
    myTonsCO2 = (8.78 * Math.pow(10 , -3)) * myGallonsUsed;

}
  ....
}

あなたが呼んでいます

CO2FootprintV1Tester footprint = new CO2FootprintV1Tester();
footprint.calcTonsCO2();

むしろそうあるべき

CO2FootprintV1 footprint = new CO2FootprintV1();
footprint.calcTonsCO2();
于 2016-01-16T23:48:17.380 に答える