我试图在我的主要调用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的符号
答案 0 :(得分:4)
如果这两个函数不在同一个类中(或者声明sortVehicles
的类不是声明此main
方法的类的超类),则需要指定调用sortVehicles
方法时声明sortVehicles
的类的名称,因此:
VehicleSorter.sortVehicles(vehicles)
或者,您可以使用sortVehicles
方法静态导入班级中的main
方法:
import static com.example.VehicleSorter.sortVehicles;
答案 1 :(得分: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;
}
}
答案 2 :(得分:0)
<class name>.<function name>
调用静态方法,例如Math.max(int , int)
。所以在你的情况下应该是:
<Class name to which sortVehicles belongs>.sortVehicles(vehicles)
否则将静态方法放在main方法所属的类中:
公共课 { 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);
}
}
}
public static void sortVehicles(Vehicle[] vehicles)
{
Arrays.sort(vehicles);
for(int i = 0; i < vehicles.length; i++)
{
System.out.println(vehicles[i]);
}
return;
}
}