0

我试图在我的 main 中调用 sort 方法,但我无法弄清楚如何正确地执行它。

public static void sortVehicles(Vehicle[] vehicles)
{
    Arrays.sort(vehicles);
    for(int i = 0; i < vehicles.length; i++)
    {
        System.out.println(vehicles[i]);
    }
    return;
}

我正在做这个......

public class 
{
    public static void main(String[] args) throws Exception {
        Vehicle[] vehicles = fillArray(args[0]);

        vehicles = fillArray(args[1]);

        System.out.print("Enter 1 to print all of the files.\nEnter 2 to sort the files by email and print.\nEnter 3 to print the amount of files.\nEnter 4 to print only bike and truck files.\nEnter 5 to print only area codes (987).\nEnter number: ");

        Scanner input = new Scanner(System.in);

        int x = input.nextInt();

        while (true) {
            if (x == 1) {
                printAll(vehicles);
            } else if (x == 2) {
                sortVehicles(vehicles);
            } else if (x == 3) {
                printRecordNumber(vehicles);
            } else if (x == 4) {
                printBicyclesAndTrucks(vehicles);
            } else if (x == 5) {
                printByAreaCode(vehicles);
            }
        }

    }// end main

它给了我一个错误,说它无法识别 sortVehicle 的符号

4

3 回答 3

4

如果这两个函数不在同一个类中(或者sortVehicles声明的类不是声明该main方法的超类),则需要sortVehicles在调用sortVehicles方法时指定声明的类的名称,因此:

VehicleSorter.sortVehicles(vehicles)

或者,您可以使用您的方法静态导入sortVehicles类中的main方法:

import static com.example.VehicleSorter.sortVehicles;
于 2013-10-18T20:38:40.760 回答
0
  • 静态方法应该用 <class name>.<function name> eg调用Math.max(int , int)

所以在你的情况下,它应该是:

<Class name to which sortVehicles belongs>.sortVehicles(vehicles)

  • 否则将静态方法放在main方法所属的类中:

    公共类 { 公共静态 void main(String[] args) 抛出异常 { Vehicle[] 车辆 = fillArray(args[0]);

        vehicles = fillArray(args[1]);
    
        System.out.print("Enter 1 to print all of the files.\nEnter 2 to sort the files by email and print.\nEnter 3 to print the amount of files.\nEnter 4 to print only bike and truck files.\nEnter 5 to print only area codes (987).\nEnter number: ");
    
        Scanner input = new Scanner(System.in);
    
        int x = input.nextInt();
    
        while (true) {
            if (x == 1) {
                printAll(vehicles);
            } else if (x == 2) {
                sortVehicles(vehicles);
            } else if (x == 3) {
                printRecordNumber(vehicles);
            } else if (x == 4) {
                printBicyclesAndTrucks(vehicles);
            } else if (x == 5) {
                printByAreaCode(vehicles);
            }
        }
    
    }
    public static void sortVehicles(Vehicle[] vehicles)
    {
        Arrays.sort(vehicles);
        for(int i = 0; i < vehicles.length; i++)
        {
             System.out.println(vehicles[i]);
        }
        return;
    }
    
    }
    
于 2013-10-18T20:42:45.617 回答
0

或者,您可以在Vehicle课堂上制作主要内容。

public class Vehicle
{
    public static void main(String[] args) throws Exception {
        Vehicle[] vehicles = fillArray(args[0]);

    vehicles = fillArray(args[1]);

    System.out.print("Enter 1 to print all of the files.\nEnter 2 to sort the files by email and print.\nEnter 3 to print the amount of files.\nEnter 4 to print only bike and truck files.\nEnter 5 to print only area codes (987).\nEnter number: ");

    Scanner input = new Scanner(System.in);

    int x = input.nextInt();

    while (true) {
        if (x == 1) {
            printAll(vehicles);
        } else if (x == 2) {
            sortVehicles(vehicles);
        } else if (x == 3) {
            printRecordNumber(vehicles);
        } else if (x == 4) {
            printBicyclesAndTrucks(vehicles);
        } else if (x == 5) {
            printByAreaCode(vehicles);
        }
    }

}// end main

public static void sortVehicles(Vehicle[] vehicles)
{
    Arrays.sort(vehicles);
    for(int i = 0; i < vehicles.length; i++)
    {
        System.out.println(vehicles[i]);
    }
    return;
}

}
于 2013-10-18T20:38:37.600 回答