Java 类内平均值方法不能应用于给定类型

Java 类内平均值方法不能应用于给定类型,java,compiler-errors,Java,Compiler Errors,我的程序中出现了上述错误。它在课程中突出显示了这一行代码: 返回s1.average+s1.average+s1.average+s1.average+s1.average/5.0 这是课程课。括号被注释掉的原因是我的导师告诉我要这样做 public class Course extends Student { private String course; private Student s1, s2, s3, s4, s5; private int studentcount = 1; pub

我的程序中出现了上述错误。它在课程中突出显示了这一行代码:

返回s1.average+s1.average+s1.average+s1.average+s1.average/5.0

这是课程课。括号被注释掉的原因是我的导师告诉我要这样做

public class Course extends Student
{
private String course;
private Student s1, s2, s3, s4, s5;
private int studentcount = 1;

public Course (String name)
{
   super(); //
   course = name;

}

public Student addStudent(String first, String last, Address home, Address school)
{

//if (studentcount == 1){
  s1 = new Student(first,last,home,school);
  studentcount++;          
  return s1;
//}    

 //if (studentcount == 2) {
  s2 = new Student(first,last,home,school);
      studentcount++;
  return s2;

//}
//else if (studentcount == 3){
  s3 = new Student(first,last,home,school);
  studentcount++;
      return s3;

//}
// else if (studentcount == 4){
  s4 = new Student(first,last,home,school);
      studentcount++;
  return s4;

//}
//else if (studentcount == 5) {
  s5 = new Student(first,last,home,school);
      studentcount++;
   return s5;

 //}
//else {
System.out.println("No More students allowed in the class");
return null;
//}
}

public double average()
{

}

public String roll()
{
String results = "";

if (studentcount == 1){
  results += s1.toString () +"n";
  return results;
}    

 if (studentcount == 2) {
  results += s1.toString () +"n";
  results += s2.toString () +"n";
  return results;

}
else if (studentcount == 3){
  results += s1.toString () +"n";
  results += s2.toString () +"n";
  results += s3.toString () +"n";
  return results;

}
else if (studentcount == 4){
  results += s1.toString () +"n";
  results += s2.toString () +"n";
  results += s3.toString () +"n";
  results += s4.toString () +"n";
  return results;

}
else if (studentcount == 5) {
  results += s1.toString () +"n";
  results += s2.toString () +"n";
  results += s3.toString () +"n";
  results += s4.toString () +"n";
  results += s5.toString () +"n";

  return results;
    } 
    else{
  return null;
}   

}
}
这是学生班:

public class Student 
{
Address home = new Address("1027 Charleston St","Lincoln", "Ne", 68508);
Address school = new Address("1534 E St", "Lincoln", "Ne", 68508);
Student mike = new Student("Mike", "Vinci", home, school);
Student john = new Student("John", "Doe", home, school, 90, 80, 199);

//Below are the ints used
int test1 = 0;
int test2 = 0;
int test3 = 0;
int avg2;
int error = 0;

public Student(){
     //empty
}

public void setTestScore(int testNum, int score)
{
    if (testNum == 1)
        test1 = score;
    else if (testNum == 2)
        test2 = score;
    else if (testNum == 3)
        test3 = score;
}

public int getTestScore(int testNum)
{
    if (testNum == 1)
        return test1;
    else if (testNum == 2)
        return test2;
    else if (testNum == 3)
        return test3;
    else
         return error;
}

public double average(int test1, int test2, int test3)
{
    int avg2 = ((test1 + test2 + test3)/3); //finds the average of the tests
    return avg2;
}

private String firstName, lastName; //private ints for coding
private Address homeAddress, schoolAddress;

public Student (String first, String last, Address home, Address school) 
{
    firstName = first;
    lastName = last;
    homeAddress = home;
    schoolAddress = school;
}

public Student (String first, String last, Address home, Address school, int test11, int test22, int test33) 
{
    firstName = first;
    lastName = last;
    homeAddress = home;
    schoolAddress = school;
    test11 = test1;
    test22 = test2;
    test33 = test3;
}

public String toString() 
{
    String result;
    result = firstName + " " + lastName + "\n";
    result += "Home Address:\n" + homeAddress + "\n";
    result += "School Address:\n" + schoolAddress + "\n";
    result += "Average=" + avg2 + " with Tests: " + test1 + ", " + test2 + ", " + test3;
    return result; //returns the result
}

class Address
{
private String streetAddress, city, state;
private long zipCode;

public Address(String street, String town, String st, long zip)
{
    streetAddress = street;
    city = town;
    state = st;
    zipCode = zip;
}

public String toString()
{
    String result;
    result = streetAddress + "\n";
    result += city + "," + state + " " + zipCode;
    return result; //returns the result
}
} 
}

因此,问题是如何修复此错误?

错误的答案是删除平均方法的参数,前提是在调用该方法之前将设置test1、test2和test3值。也许您应该检查其中的空值

public int average()
{
    int avg2 = ((test1 + test2 + test3)/3); //finds the average of the tests
    return avg2;
}
尽管在上述方法中返回double可能更适用,如前所述:

由于将avg2声明为实例变量,因此可以使用average对其进行更新,以供以后使用。对于当前代码,avg2永远不会为打印输出设置,因为您正在创建一个仅与方法作用域相关的新变量:int avg2=。。。一旦返回,其值将被丢弃

我建议进行以下更改:

将avg2从整数更改为双精度 从平均值内设置该类变量 可视化:

double avg2;

...

public int average()
{
    avg2 = ((test1 + test2 + test3)/3); //finds the average of the tests
    return avg2;
}
虽然您也可以在同一行中将avg2实例化为零,但您可以声明它并使用average进行简单更新:

因此,在课程类的双平均法中,您可以调用:

public double average()
{
    return (s1.average() + s2.average() + s3.average() + s4.average() + s5.average()) / 5.0;
}
同样,推断所有值在每次调用之前填充。尽管看起来您的应用程序工作流已经基于这些假设构建。

而不是调用:

return (s1.average() + s1.average() + s1.average() + s1.average() + s1.average()) / 5.0
电话:

return (s1.average(s1.test1,s1.test2,s1.test3) + s2.average(s2.test1,s2.test2,s2.test3) + s3.average(s3.test1,s3.test2,s3.test3) + s4.average(s4.test1,s4.test2,s4.test3) + s5.average(s5.test1,s5.test2,s5.test3)) / 5.0

那么问题是什么:确切地说?您希望我们回答什么?如何修复此错误?对不起,我会在帖子中加上这句话,准确地问一个好问题——如果其他人确切地理解你在寻找什么,他们会给你更好的答案;类中的平均值方法Student需要3个参数,但您调用它时没有任何参数-这就是为什么会出现此错误。您可能应该删除Student类中该方法的参数,您希望它处理成员变量,而不是您传递的值。是否确实要为平均方法返回int。你应该考虑返回双,或者浮动,这样你可以有小数值,我不认为这是最好的方法。每个类都已经可以访问这些即时变量,为什么从每个类中检索它们只是为了将它们作为方法参数发送回来?我没有看到您的更新@Trobbins,我将不得不稍后联机
return (s1.average(s1.test1,s1.test2,s1.test3) + s2.average(s2.test1,s2.test2,s2.test3) + s3.average(s3.test1,s3.test2,s3.test3) + s4.average(s4.test1,s4.test2,s4.test3) + s5.average(s5.test1,s5.test2,s5.test3)) / 5.0