Java – 无法调用编译错误方法

我必须使用测试工具编译我的代码,但是,当该测试工具调用我的方法时,我收到此错误:

“类课程中的getCourseDetails方法不能应用于给定的类型;

required:java.lang.String,int,java.lang.String,boolean,java.lang.String.java.lang.String,double

发现:没有参数

原因:实际和正式的参数列表长度不同。

您在此处使用的运算符不能用于您使用它的值类型。 你要么在这里使用错误的类型,要么使用错误的操作符。“

这是我的方法:

public static void getCourseDetails(String department, int number, String name, boolean isFull, String SCHOOL_NAME, String motto, double price){ if (department.length() != (0) && number != 0 && name.length() != (0) && price != 0) { System.out.print(department + " "); } else if (department.length() == (0)){ System.out.print ("Sorry, there is an error with the course department."); return; } if (number == 0) { System.out.print("Sorry, there is an error with the course number."); return; } else if (number != 0 && department.length() != (0) && name.length() != (0) && price != 0){ System.out.print(number + " "); } if (name.length() != (0) && number!= 0 && department.length() != (0) && price != 0) { System.out.println(name + "."); } else if (name.length() == (0)) { System.out.print("Sorry, there is an error with the course name."); return; } if (price == 0){ System.out.print("Sorry, there is an error with the course price."); return; } //System.out.println(department + " " + number + " is " + name); if (isFull == true){ System.out.println("The course is currently full."); } else if (isFull == false){ System.out.println("The course is currently not full."); } System.out.println("The course is currently run at " + SCHOOL_NAME + " where their motto is " + "\"" + motto + "\""); 

您的问题是您没有将适当的参数传递给方法,因此它会吐出该错误。

 public static void getCourseDetails(String department, int number, String name, boolean isFull, String SCHOOL_NAME, String motto, double price){ 

对于此代码,您需要以相同的顺序传递所有这些变量(第一个字符串,第二个int等)。 你不能只是所有getCourseDetails()里面没有任何东西,并期望发生一些事情,因为你试图在该方法中处理的所有信息实际上都没有进入其中。

因此,例如,当您调用此方法时,它可能看起来像这样

 String department = "Math"; int number = 101; String name = "Williams"; boolean isFull = false; String SCHOOL_NAME = "Pinkerton High" String motto = "We Never Sleep" double price = 100.0; //note that the variable names do not have to be the same here //as they are in the method getCourseDetails(department, number, name, isFull, SCHOOL_NAME, motto, price);