我是编码的新手。我正在使用BlueJ并且下面的代码编译得很好但是当我尝试使用add
方法时,我得到一个非法的表达式开始。
据我所知,我没有嵌套在主要或任何东西的方法。
寻求一点帮助。只想测试add方法,其他方法还没有真正完成。
import java.util.ArrayList;
public class Roster
{
static ArrayList<Student> myRoster = new ArrayList<>();
public static void main(String[] args)
{
printAll();
}
public static void add(int studentID, String firstName, String lastName, String eMail, int age, int grade1, int grade2, int grade3)
{
int[] grades = {grade1, grade2, grade3};
Student newStudent = new Student(studentID, firstName, lastName, eMail, age, grades);
myRoster.add(newStudent);
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public static void remove(int studentID)
{
}
public static void printAll()
{
for(int i = 0; i < myRoster.size(); i++)
{
myRoster.get(i).print();
}
}
public static void printAverageGrade(int studentID)
{
for(Student eachStudent : myRoster)
{
double average = (eachStudent.getGrades()[0] + eachStudent.getGrades()[1] + eachStudent.getGrades()[2]) / 3.0;
System.out.println("Student ID: " + eachStudent.getStudentID() + " Average Grade: " + average);
}
}
public static void printInvalidEmails()
{
}
}
这是学生班,我得到的信息是在界面上(新的,不知道它叫什么,见下一句),上面写着“错误:非法开始表达”。当我右键单击类名册并转到add方法时会发生这种情况。然后我输入输入并得到错误。它在输入Roster.add()之前在顶部显示状态,这对我来说似乎不对。不应该说myRoster.add?
如果它有帮助,从代码板(我甚至不知道这是否是我想添加“学生数据”的地方)如果我手动输入添加方法它会得到一个无法找到符号的错误 - 方法添加(.....)
public class Student
{
private String firstName;
private String lastName;
private int studentID;
private String eMail;
private int age;
private int[] grades;
/**
* Constructor for objects of class Student
*/
public Student( int studentID, String firstName, String lastName, String eMail, int age, int[] grades)
{
setStudentID(studentID);
setFirstName(firstName);
setLastName(lastName);
setEMail(eMail);
setAge(age);
setGrades(grades);
}
/**
* Gets the student's ID
* @return studentID
*/
public int getStudentID()
{
return studentID;
}
//Mutator
public void setStudentID(int studentID)
{
this.studentID = studentID;
}
/**
* Gets the first name of the student
* @return firstName
*/
public String getFirstName()
{
return firstName;
}
//Mutator
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
/**
* Gets the last name of the student
* @return lastName
*/
public String getLastName()
{
return lastName;
}
//Mutator
public void setLastName(String lastName)
{
this.lastName = lastName;
}
/**
* Gets the eMail of the student
* @return eMail
*/
public String getEMail()
{
return eMail;
}
//Mutator
public void setEMail(String eMail)
{
this.eMail = eMail;
}
/**
* Gets the student's age
* @return age
*/
public int getAge()
{
return age;
}
//Mutator
public void setAge(int age)
{
this.age = age;
}
/**
* Gets the student's grades
* @return grades
*/
public int[] getGrades()
{
return grades;
}
//Mutator
public void setGrades(int[] grades)
{
this.grades = grades;
}
public void print()//prints specific student data
{
System.out.println("Student ID:\t" + getStudentID() +
"\tFirst Name:\t" + getFirstName() +
"\tLast Name:\t" + getLastName() +
"\tEmail:\t" + getEMail() +
"\tGrade1:\t" + getGrades()[0] +
"\tGrade2:\t" + getGrades()[1] +
"\tGrade3:\t" + getGrades()[2]);
}
}