小编典典

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

java

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

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

必需: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 + "\"");

阅读 314

收藏
2020-12-03

共1个答案

小编典典

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

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);
2020-12-03