当前位置: 首页 > 知识库问答 >
问题:

在Java中的数组上运行方法[重复]

慕乐语
2023-03-14

得到了经典作业,我需要创建学生和课程类以及驾驶员类。

我在计算我的课程班级的整体平均成绩时遇到了问题。一切都正常,但当我运行驱动程序时,我得到了“0”作为类平均值。在IDE中,它告诉我s中的“s”。getAverage();'在getClassAverage方法中无法解决,但我不确定为什么,因为我在其他地方使用它很好。我试着遍历一组学生,把他们的平均数加起来。

有什么指示吗?谢谢你的时间!

大学生JAVA

public class Student
{
private String firstName, lastName;
private int testScore1, testScore2, testScore3;
private int studentAverage;

//Constructors
public Student()
{
    firstName = "None";
    lastName = "None";
    testScore1 = 0;
    testScore2 = 0;
    testScore3 = 0;
    studentAverage = 0;
}

public Student(String first, String last, int score1, int score2, int score3)
{
    firstName = first;
    lastName = last;
    testScore1 = score1;
    testScore2 = score2;
    testScore3 = score3;
}

//Setter for test scores
public void setTestScore(int testNum, int score)
{
    if (testNum == 1)
        testScore1 = score;
    else
        if (testNum == 2)
            testScore2 = score;
        else
          if (testNum == 3)
            testScore3 = score;
          else
            throw new IllegalArgumentException(testNum + " is out of range");
    }
//Getter for test scores
public int getTestScore(int testNum2)
{
    if (testNum2 == 1)
        return testScore1;
    else
        if (testNum2 == 2)
            return testScore2;
        else
          if (testNum2 == 3)
            return testScore3;
          else
            throw new IllegalArgumentException(testNum2 + " is out of range");
    }

//Getter for student first name and last name
public String getFirstName()
{
    return firstName;
}

public String getLastName()
{
    return lastName;
}

//Calculates average for each student
public int getAverage()
{
    int studentAverage = (testScore1 + testScore2 + testScore3)/3;
    return studentAverage;
}
//Returns a description of this Student object
public String toString()
{
    String result;

    result = firstName + " " + lastName + "\n";
    result += "Test Score 1:\n" + testScore1 + "\n";
    result += "Test Score 2:\n" + testScore2 + "\n";
    result += "Test Score 3:\n" + testScore3 + "\n";
    result += "Average Test Score:\n" + studentAverage;

    return result;
}
}

课程JAVA

import java.util.ArrayList;

public class Course {
//What Course class knows
private String courseName;
private int classAverage;
private int studentAverage, sumAverages;
private ArrayList<Student> students;

public static int count = 0;

//Constructor
public Course(String courseName)
{
    this.courseName = courseName;

    students = new ArrayList<Student>();
}

//Add students
public void addStudent(Student s)
{
    students.add(s);
    ++count;
}

//Return average of all student test score averages
public int getClassAverage()
{
    for(Student s : students);
    {
        sumAverages += s.getAverage();
    }

    int classAverage = (int) sumAverages/students.size();
    return classAverage;
}


//Roll call - prints all students in the course
public void roll()
{
    System.out.println("Course: " + courseName);
    System.out.println("Number of Students: " + count);
    System.out.println("Students: ");

    for(Student s : students) 
    {
        System.out.println(s.getFirstName() + " " + s.getLastName());
    }
}

//Prints class average
public String toString()
{
    String result;
    result = "The class average is: "+ classAverage;
    return result;
}

}

司机

public class CourseDriver {

public static void main(String[] args) {
    //Create a new course
    Course course1 = new Course("CSC 140");

    //Create and add several students to course
    course1.addStudent( new Student("Sarah", "Mauer", 93, 82, 67) );
    course1.addStudent( new Student("Andrew", "Kagan", 85, 80, 75) );
    course1.addStudent( new Student("Carly", "Sanseverino", 95, 83, 80) );

    //Prints roll
    course1.roll();

    System.out.println(course1);
}

}

共有1个答案

司马高韵
2023-03-14

for循环后的分号结束了“s”的作用域。去掉那个,一切都会好起来的。

 类似资料:
  • 我有一个扩展Person类的学生类。我的超级类中没有equals方法,只有我的子类中的两个,如图所示。我正在尝试理解我的第二、第三和第四个print语句的运行时行为。语句1调用了接受学生参数的equals方法,这是有意义的,因为要比较的两个对象都是声明类型的学生。但是,语句2调用了接受人参数的equals方法,而最后两个语句调用了Object类中的equals方法。有人能解释一下为什么Java是动

  • 有人能彻底解释一下为什么这会打印21003和32103吗?我真的被这弄糊涂了。谢谢你!

  • 我正在为这学期的java期末考试做练习,我正在做一些书中的问题。 编写一个方法,如果数组list1和list2完全相同,则返回true,使用以下标题: 公共静态布尔等于(int[]list1,int[]list2) 这是我的密码。它在返回语句旁边给出了“非法的语句开始”。我做错了什么?

  • 问题内容: 我有一个返回String的方法,是否有可能在一段时间后超出该阈值以返回该特定字符串? 问题答案: 该番石榴库有一个非常好的,可以让你做到这一点,就会向由接口定义的任何方法。它可以为具有“内置”超时的对象生成代理。

  • 看起来有两种路径,Eclipse无法读取代码/识别类。

  • 我有一个简单的Java代码: 文件夹结构为: 在文件夹中,有和文件。 null null 这里怎么了?