This question already has answers here:
Cannot find symbol Java error?
                                
                                    (3个答案)
                                
                        
                4年前关闭。
            
        

当我编译AnnualFuelUseTester时,我得到一个错误,提示“找不到符号-方法findMaxDistance(AnnualFuelUse [])”,而实际上我确实创建了一个findMaxDistance静态方法。该代码分为两个类,如下所示

public class AnnualFuelUseTester
{
    // main method
    public static void main(String[ ] args)                                          // main method
    {
    //create 4 new AnnualFuelUseTester objects
    AnnualFuelUse[] car1 = {new AnnualFuelUse(1, 10876, 11665, 29.00, 1.99),      // object data for the first fill up
                            new AnnualFuelUse(13, 11665, 12399, 27.60, 1.97),     // object data for the second fill up
                            new AnnualFuelUse(28, 12399, 13155, 26.99, 2.05),     // object data for the thrid fill up
                            new AnnualFuelUse(40, 13155, 13946, 29.47, 2.03)};    // object data for the fourth fill up

    //call methods
    for(int index = 0; index < car1.length; index++)                // calls the follwing methods for each car1 object
    {
        car1[index].calcDistance();                                 // call method to calculate the distance
        car1[index].calcMPG();                                      // call method to calculate the MPG
        car1[index].totalCost();                                    // call method to calculate the total cost
    }

    // print the data
    System.out.printf("%7s%7s%14s%12s%11s%15s%7s%8s%8s\n",
        "Fillup", "Days", "Start Miles", "End Miles", "Distance", "Gallons Used", "MPG", "Price", "Cost");                                    // print the headers
    for(int index = 0; index < car1.length; index++)
    {
        System.out.printf("   %-4s%6d%12d%13d%11d%9d%14.2f%10.1f%9.2f%9.2f\n",
            car1[index].getDays(), car1[index].getStartMiles(), car1[index].getEndMiles(), car1[index].getDistance(),
            car1[index].getGallonsUsed(), car1[index].getMilesPerGallon(), car1[index].getPricePerGallon(), car1[index].getTotalPrice());     // prints and formates car data
    }
    System.out.printf("%8s%36d%24.1f%9.2f\n", "Minimum", findMinDistance(car1), findMinMPG(car1), findMinPrice(car1));                        // prints and formats the mimimun data
    System.out.printf("%8s%36d%24.1f%9.2f\n", "Maximun", findMaxDistance(car1), findMaxMPG(car1), findMaxPrice(car1));                        // prints and formats the mimimun data
  }  //end of main method
}




public class AnnualFuelUse
{
// variable declaration and initialization
private int myDays, myStartMiles, myEndMiles, myDistance;
private double myGallonsUsed, myPricePerGallon, myMilesPerGallon, myTotalPrice;

// default constructor
AnnualFuelUse()
{
}

// constructor that accepts all car descriptors
AnnualFuelUse(int days1, int startMiles1, int endMiles1, double gallonsUsed1, double pricePerGallon1)
{
    myDays = days1;                                         // creates a parameter to use for car type
    myEndMiles = endMiles1;                                 // creates a parameter to use for end miles
    myStartMiles = startMiles1;                             // creates a parameter to use for start miles
    myGallonsUsed = gallonsUsed1;                           // creates a parameter to use for galons used
    myPricePerGallon = pricePerGallon1;                     // creates a parameter to use for price per gallon
    myDistance = 0;                                         // creates a parameter to use for distance
    myMilesPerGallon = 0.0;                                 // creates a parameter to use for mpg
    myTotalPrice = 0.0;                                     // creates a parameter to use for total price
}

// calculate the Distance driven
public void calcDistance()                                  // method returns a interger distance
{
    myDistance = myEndMiles - myStartMiles;                 // calculates and returns the distance to the main method
}

// calculate the miles per gallon
public void calcMPG()                                       // method returns a double MPG and accepts a interger distance
{
    myMilesPerGallon = (double)myDistance / myGallonsUsed;  // calculates and returns the mpg to the main method
}

// calcualtes the total cost
public void totalCost()                                     // method that returns a double cost
{
    myTotalPrice = myGallonsUsed * myPricePerGallon;        // calculates the price per gallons and returns it to the main method
}

// method to return myDays
public int getDays()
{
    return myDays;                                          // returns myDays to the main method
}

// method to return myEndMiles
public int getEndMiles()
{
    return myEndMiles;                                      // returns myEndMiles to the main method
}

// method to return myStartMiles
public int getStartMiles()
{
    return myStartMiles;                                    // returns myStartMiles to the main method
}

// method to return myGallonsUsed
public double getGallonsUsed()
{
    return myGallonsUsed;                                   // returns myGallonsUsed to the main method
}

// method to return myPricePerGallon
public double getPricePerGallon()
{
    return myPricePerGallon;                                // returns myPricePerGallon to the main method
}

// method to return myDistance
public int getDistance()
{
    return myDistance;                                      // returns myDistance to the main method
}

// method to return myMilesPerGallon
public double getMilesPerGallon()
{
    return myMilesPerGallon;                                // returns myMilesPerGallon to the main method
}

// method to return myTotalPrice
public double getTotalPrice()
{
    return myTotalPrice;                                    // returns myTotalPrice to the main method
}

// calculate the min distance
public static int findMinDistance(AnnualFuelUse[] car1)
{
    int min = Integer.MAX_VALUE;                            // set min at a really big number
    for(int index = 0; index < car1.length; index++)
    {
        if(min > car1[index].getDistance())
        {
            min = car1[index].getDistance();
        }
    }
    return min;
}

public static int findMaxDistance(AnnualFuelUse[] car1)
{
    int max = Integer.MIN_VALUE;                            // set min at a really small number
    for(int index = 0; index < car1.length; index++)
    {
        if(max < car1[index].getDistance())
        {
            max = car1[index].getDistance();
        }
    }
    return max;
}

public static double findMinMPG(AnnualFuelUse[] car1)
{
    double min = Double.MAX_VALUE;                            // set min at a really big number
    for(int index = 0; index < car1.length; index++)
    {
        if(min > car1[index].getDistance())
        {
            min = car1[index].getDistance();
        }
    }
    return min;
}

public static double findMaxMPG(AnnualFuelUse[] car1)
{
    double max = Double.MIN_VALUE;                            // set min at a really small number
    for(int index = 0; index < car1.length; index++)
    {
        if(max < car1[index].getDistance())
        {
            max = car1[index].getDistance();
        }
    }
    return max;
}

public static double findMinPrice(AnnualFuelUse[] car1)
{
    double min = Double.MAX_VALUE;                            // set min at a really big number
    for(int index = 0; index < car1.length; index++)
    {
        if(min > car1[index].getDistance())
        {
            min = car1[index].getDistance();
        }
    }
    return min;
}

public static double findMaxPrice(AnnualFuelUse[] car1)
{
    double max = Double.MIN_VALUE;                            // set min at a really small number
    for(int index = 0; index < car1.length; index++)
    {
        if(max < car1[index].getDistance())
        {
            max = car1[index].getDistance();
        }
    }
    return max;
}
}

最佳答案

findMaxDistance是在AnnualFuelUse类中定义的,而不是在AnnualFuelUseTester中定义的。因此,您需要以静态方式引用该方法,即使用此处提到的类名

AnnualFuelUse.findMaxDistance(car1)

关于java - 调用将对象数组作为参数的静态方法时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34325413/

10-12 00:23
查看更多